XII WorkingWithFunction
XII WorkingWithFunction
in Python
Note: The function calling another function is called the caller and the function being called
is the called function(or callee). Here, the __main__ is the caller of func() function.
Arguments and Parameters
Example We see that there are values being passed
def multiply(a, b): (through function call) and values being
received (in function definition)
print(a*b)
Arguments: Python refer to the values being
y=3 passed as arguments.
multiply(12, y) Parameters: values being received as
multiply(y, y) parameters
Parameter or formal
multiply(y, x) parameter
Arguments or actual
arguments
Note: The values being passed through a function call statement are called arguments(or
actual parameters or actual arguments). The values received in the function
definition/header are called parameter or formal parameter or formal arguments)
Passing Parameters
Python supports three types of formal parameters:
1. Positional arguments(Required arguments)
2. Default arguments
3. Keyword( or named ) arguments
Positional Arguments(required arguments)
If a function definition header is like:
def check(a, b, c):
interest(prin=2000, time=2, rate = 0.10) : Here, prin gets value 2000, time gets value as 2 and rate as 0.10
interest(time=4, prin=2600, rate = 0.09) : Here, prin gets value 2600, time gets value as 4 and rate as 0.09
interest(time=2, rate = 0.12, prin=2000,) : Here, prin gets value 2000, time gets value as 2 and rate as 0.1
Using Multiple Argument Types Together
Example: The first argument value (5000) in above statement is representing a positional argument as
it will be assigned to first parameter on the basis of its position.
interest(5000, time = 5)
The second argument value (time = 5) in above statement is representing keyword argument or
named argument.
Rules for combining all three types of arguments
• Python states that in a function call statements:
• An argument list must first contain positional (required ) arguments followed by any keyword argument.
• Keyword arguments should be taken from required arguments preferably.
• You cannot specify a value for an argument more than once.
Note: Having a positional arguments after keyword arguments will result into error.
Returning Values From Functions
• Functions in python may or may not return a value.
• There can be broadly two types of functions in Python:
Function returning some values (non-void functions)
Functions not returning any value ( void function)
Note: A void function (some times called non-fruitful function) returns legal empty value
of Python i.e. None to its caller.
Returning Multiple Values
To return multiple values from a function, we have to ensure following
things:
i) The return statement inside a function body should be of the form
given below:
return <value1 / varable1 / expression1>, <value2 / varable2 /
expression2>,……..
ii) The function call statement should receive or use the returned value
in one of the following ways:
Returning Multiple Values
ii) The function call statement should receive or use the returned value in one of the
following ways:
COMPOSITION
Composition in general refers to using an expression as part of a larger
expression or
Statement as a part of larger statement
An arithmetic expression e.g.
greater((4+5),(3+4))
A logical expression e.g.
test(a or b)
A function call (function composition) e.g.
int(str(52))
int(float(“52.5”)*2)
int(str(52) + str(10))
Scope of variables
• Two kinds of scopes in Python are
1. Global Scope
A name is declared in top level segment( __main__) of a program is
said to have a global scope and is usable inside the whole program
and all blocks(functions, other blocks) contained within the program.
2. Local Scope
A name declared in a function-body is said to have local scope i.e.
it can be used only within this function sand the other blocks
contained under it.
The names of formal arguments also have local scope.
Scope Example 1
1. def calcSum (x, y):
2. z=x+y
3. return z
4. num1 = int(input(“enter first number:”))
5. num2 = int(input(“Enter second number :”))
6. Sum = calcSum(num1, num2);
7 print(‘sum of given number is’, sum)
In the above program there are three variables num1, num2 and sum defined in the
main program and three variables x,y and z defined in the function calcSum().
num1,num2 and sum are global variables and x,y and z are local variables( local to
function calcSum() ).
Scope Example 1
Scope Example 1
Scope of variables
Variable defined outside all functions are global.
Example
x=5
def func(a) :
b=a+1 Variables x defined above all functions. It is also a global variable
return b along with y and z.
y = input(“Enter number”)
z = y + func(x)
print(z)
Name Resolution(Resolving Scope of a Name)
Python follow NAME Resolution Rule, also known as LEGB rule.
L. Local - It first makes a local search Example:
i.e. in current def statement. The import x=5
statements and function definitions bind def func1():
x=2
the module or function name in the local
x=x+1
scope. In fact, all operations that
def func2():
introduce new names use the local global x
scope. x=x+1
E. Enclosing functions - It searches in print x
all enclosing functions, form inner to func1()
outer. print x
G. Global (module) - It searches for func2()
global modules or for names declared print x
global in a def within the file. The above example prints 5; then calling
B. Built-in (Python) - Finally it checks func1()it prints 3. This is because func1 only
for any built in functions in Python. increments a local x. Then func2() increments
the global x and prints 6.
Thanks to all……