KEMBAR78
3 cs xii_python_functions _ parameter passing | PPTX
.
Class-XII Computer Science (083)
All the contents used as part of the slides are either self created or from the public domain or textbooks for Class XII.
This presentation is only used for leaning purpose only. Programs used in this presentation are based on Python 3.8.0.
Computational Thinking and Programming - 2
Working with Functions
Parameter Passing
S K Mahto, PGT (Computer Science)
J.N.V East Medinipur WB
Working with Functions
Parameter Passing
● We can pass values to functions, for this we need to define variables to receive
values in function definition and we can send value via a function call statement.
● For example
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
● Pass by reference vs. value
● All parameters (arguments) in the Python language are passed by reference.
● It means if we change what a parameter refers to within a function, the change
also reflects back in the calling function.
● For example −
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
● Pass by reference vs. value
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
● Pass by reference vs. value --- continue
Presented by : S K Mahto, PGT Computer Science
There is one more example
where argument is being
passed by reference and the
reference is being overwritten
inside the called function.
Working with Functions
Parameter Passing
def fact(num):
f=1
for i in range(num,0,-1):
f=f*i
return(f)
print(fact(5))
Body of the
function
Name of
the
function
Argument of the function
Function call
OR
Calling function
Presented by : S K Mahto, PGT Computer Science
Actual Parameter
Vs.
Formal Parameter
Here num is formal parameter/ argument
(parameter used in function
definition is called formal parameter)
Here 5 is actual parameter/ argument
(parameter used in calling function
is called actual parameter)
Working with Functions
Parameter Passing
● Python supports three types of formal arguments/ parameters :
1. Positional (required) arguments
2. Default arguments
3. Keyword (or named) arguments
4. Variable length arguments
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Positional (required) arguments
Positional (Required) arguments are the arguments passed to a function in correct positional order. Here, the
number of arguments in the function call should match exactly with the function definition.
Presented by : S K Mahto, PGT Computer Science
To call the function printme(),
we definitely need to pass one
argument otherwise it gives a
syntax error as follows -
Working with Functions
Parameter Passing
Default arguments
● A default argument is an argument that assumes a default value if a
value is not provided in the function call for that argument. The
following example gives an idea on default arguments, it prints
default age if it is not passed −
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Default arguments
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Keyword arguments
● Keyword arguments are related to the function calls. When we use keyword
arguments in a function call, the caller identifies the arguments by the parameter
name.
● This allows us to skip arguments or place them out of order because the Python
interpreter is able to use the keywords provided to match the values with
parameters.
● We can also make keyword calls to the printme() function in the following ways −
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Keyword arguments
Presented by : S K Mahto, PGT Computer Science
Output
Working with Functions
Parameter Passing
Keyword arguments -- continue…..
● The following example gives more clear picture.
● Note that the order of parameters does not matter.
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Rules for combining all three types of arguments-
Python states that in a function call statement :
● An argument list must first contain positional (required ) arguments followed by
any keyword argument.
● Keyword arguments should be taken from the required arguments preferably.
● We can not specify a value for an argument more than once.
Example….
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Rules for combining all three types of arguments-
Presented by : S K Mahto, PGT Computer Science
Output
Working with Functions
Parameter Passing
Variable length arguments
● You may need to process a function for more arguments than you specified while
defining the function. These arguments are called variable-length arguments and
are not named in the function definition, unlike required and default arguments.
● Syntax for a function with non-keyword variable arguments is this −
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword
variable arguments. This tuple remains empty if no additional arguments are specified during
the function call. Example…..
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
Variable length arguments
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
RETURNING VALUES FROM FUNCTIONS
Functions may or may not return a value.
1. Functions returning some value (Non-void functions)
return(value) OR return value
value may be - literal / variable / expression
2. Functions not returning any value (Void functions)
return
Function styles –
1. No argument, No return value
2. No argument with return value
3. Argument , Without return value
4. Argument with return value
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
RETURNING MULTIPLE VALUES
To return multiple values from function we have to ensure the following things –
1. The return statement inside a function body in the form of -
return value1, value2,…
2. The function call statement should receive or use the returned values in one of
the following ways –
- in the form of tuple variable
def squared(2,3,4):
return x*x,y*y,z*z
t=squared(2,3,4)
print(t)
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Parameter Passing
RETURNING MULTIPLE VALUES
- by specifying the same number of variable on left hand side of the
assignment in function call.
def squared(2,3,4):
return x*x,y*y,z*z
v1,v2,v3=squared(2,3,4)
print(v1,v2,v3)
Presented by : S K Mahto, PGT Computer Science

3 cs xii_python_functions _ parameter passing

  • 1.
    . Class-XII Computer Science(083) All the contents used as part of the slides are either self created or from the public domain or textbooks for Class XII. This presentation is only used for leaning purpose only. Programs used in this presentation are based on Python 3.8.0. Computational Thinking and Programming - 2 Working with Functions Parameter Passing S K Mahto, PGT (Computer Science) J.N.V East Medinipur WB
  • 2.
    Working with Functions ParameterPassing ● We can pass values to functions, for this we need to define variables to receive values in function definition and we can send value via a function call statement. ● For example Presented by : S K Mahto, PGT Computer Science
  • 3.
    Working with Functions ParameterPassing ● Pass by reference vs. value ● All parameters (arguments) in the Python language are passed by reference. ● It means if we change what a parameter refers to within a function, the change also reflects back in the calling function. ● For example − Presented by : S K Mahto, PGT Computer Science
  • 4.
    Working with Functions ParameterPassing ● Pass by reference vs. value Presented by : S K Mahto, PGT Computer Science
  • 5.
    Working with Functions ParameterPassing ● Pass by reference vs. value --- continue Presented by : S K Mahto, PGT Computer Science There is one more example where argument is being passed by reference and the reference is being overwritten inside the called function.
  • 6.
    Working with Functions ParameterPassing def fact(num): f=1 for i in range(num,0,-1): f=f*i return(f) print(fact(5)) Body of the function Name of the function Argument of the function Function call OR Calling function Presented by : S K Mahto, PGT Computer Science Actual Parameter Vs. Formal Parameter Here num is formal parameter/ argument (parameter used in function definition is called formal parameter) Here 5 is actual parameter/ argument (parameter used in calling function is called actual parameter)
  • 7.
    Working with Functions ParameterPassing ● Python supports three types of formal arguments/ parameters : 1. Positional (required) arguments 2. Default arguments 3. Keyword (or named) arguments 4. Variable length arguments Presented by : S K Mahto, PGT Computer Science
  • 8.
    Working with Functions ParameterPassing Positional (required) arguments Positional (Required) arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. Presented by : S K Mahto, PGT Computer Science To call the function printme(), we definitely need to pass one argument otherwise it gives a syntax error as follows -
  • 9.
    Working with Functions ParameterPassing Default arguments ● A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it prints default age if it is not passed − Presented by : S K Mahto, PGT Computer Science
  • 10.
    Working with Functions ParameterPassing Default arguments Presented by : S K Mahto, PGT Computer Science
  • 11.
    Working with Functions ParameterPassing Keyword arguments ● Keyword arguments are related to the function calls. When we use keyword arguments in a function call, the caller identifies the arguments by the parameter name. ● This allows us to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. ● We can also make keyword calls to the printme() function in the following ways − Presented by : S K Mahto, PGT Computer Science
  • 12.
    Working with Functions ParameterPassing Keyword arguments Presented by : S K Mahto, PGT Computer Science Output
  • 13.
    Working with Functions ParameterPassing Keyword arguments -- continue….. ● The following example gives more clear picture. ● Note that the order of parameters does not matter. Presented by : S K Mahto, PGT Computer Science
  • 14.
    Working with Functions ParameterPassing Rules for combining all three types of arguments- Python states that in a function call statement : ● An argument list must first contain positional (required ) arguments followed by any keyword argument. ● Keyword arguments should be taken from the required arguments preferably. ● We can not specify a value for an argument more than once. Example…. Presented by : S K Mahto, PGT Computer Science
  • 15.
    Working with Functions ParameterPassing Rules for combining all three types of arguments- Presented by : S K Mahto, PGT Computer Science Output
  • 16.
    Working with Functions ParameterPassing Variable length arguments ● You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments. ● Syntax for a function with non-keyword variable arguments is this − An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. Example….. Presented by : S K Mahto, PGT Computer Science
  • 17.
    Working with Functions ParameterPassing Variable length arguments Presented by : S K Mahto, PGT Computer Science
  • 18.
    Working with Functions ParameterPassing RETURNING VALUES FROM FUNCTIONS Functions may or may not return a value. 1. Functions returning some value (Non-void functions) return(value) OR return value value may be - literal / variable / expression 2. Functions not returning any value (Void functions) return Function styles – 1. No argument, No return value 2. No argument with return value 3. Argument , Without return value 4. Argument with return value Presented by : S K Mahto, PGT Computer Science
  • 19.
    Working with Functions ParameterPassing RETURNING MULTIPLE VALUES To return multiple values from function we have to ensure the following things – 1. The return statement inside a function body in the form of - return value1, value2,… 2. The function call statement should receive or use the returned values in one of the following ways – - in the form of tuple variable def squared(2,3,4): return x*x,y*y,z*z t=squared(2,3,4) print(t) Presented by : S K Mahto, PGT Computer Science
  • 20.
    Working with Functions ParameterPassing RETURNING MULTIPLE VALUES - by specifying the same number of variable on left hand side of the assignment in function call. def squared(2,3,4): return x*x,y*y,z*z v1,v2,v3=squared(2,3,4) print(v1,v2,v3) Presented by : S K Mahto, PGT Computer Science