Functions In
Python
Function is the term derived from maths and
means routine (a piece of code) that may or
may not accept arguments and after execution
provides a result. In Python, blocks of code
can be named. Such named blocks of code can
be run from elsewhere in your program, and
are known as functions.
Defining a Function
● Function blocks begin with the keyword def
followed by the function name , parentheses and
colon (:)
● Any input parameters or arguments should be
placed within these parentheses.
● The code block should be indented.
● The statement return [expression] exits the
function , optionally passing back an expression to
the caller. A return statement with no arguments is
the same as return None.
● Order of the parameters should be the same order
Example : 1
# Function definition without parameters
def greetings():
print ("Hello , this is my first function based pgm ")
return # Calling function begins here
greetings()
print ("Good bye")
Output : Hello , this is my first function based pgm
Good bye
Arguments and Parameters
1. Positional (Required) Arguments
2. Default Arguments
3. Named (keyword) Arguments
4. Arbitrary Arguments
Positional (Required) Arguments
When the function call statement
must match the number and order of
arguments as defined in the function
definition, this is called the positional
argument matching.
Default Arguments
A parameter
having a default
value in function
header becomes
optional in
function call.
Function call may
or may not have
value for it.
Named (keyword) Arguments
Keyword arguments are
the named arguments with
assigned values being
passed in the function call
statement.
Arbitrary Arguments
def AbtArgu(*args):
print(args)
AbtArgu(1,2,3,4,5)
1. def AbtArgu(a,b, *args):
print(args)
AbtArgu(1,2,3,4,5)
2. def AbtArgu(a,b,*args):
print(args,kwarg)
AbtArgu(1,2,3,4,5,a='hi',b='bye')
3. def AbtArgu(*args,**kwarg):
print(args,kwarg)
AbtArgu(1,2,3,4,5,a='hi',b='bye')
def Callme(Tic, Tac, Toe=5, Ta=6):
return …………..
1. Callme(Tic=3,Tac=2)
2. Callme(Ta=7,Tac=4,Tic=8)
3. Callme(Toe=1, Tic=3, Tac=5)
4. Callme(4,6,Ta=4)
5. Callme(Toe=4,7,8)
6. Callme(4,Tac=6, Tic=2)
def Callme(Tic, Tac, Toe=5, Ta=6):
return …………..
7. Callme(3,Tac=6, Toa=7)
8. Callme(3, Toe=2, Ta=2)
Scope of Variables
All variables in a program may not be accessible at all locations
in that program. This depends on where you have declared a
variable.
This means that local variables can be accessed only inside the
function in which they are declared, whereas global variables
can be accessed throughout the program body by all functions.
x = 141 # global variable
def fun():
x = 424 #local variable
print ("Local variable - Value of x inside the
fun(): " ,x)
fun()
print ("Global variable - Value of x outside the
fun(): " ,x)
Output :
Local variable - Value of x inside the fun(): 424
Global variable - Value of x outside the fun(): 141
def f():
global s
print(s)
s = "dog"
print(s)
s = "cat"
f()
print(s)
Output:
cat
dog
dog
def Hitit(L):
print("\t This is Inside function now...")
print("\t List received : ",L)
[Link](4)
[Link]([7,8])
print("\t List after adding some elements: ",L)
[Link](5)
print("\t within function after all changes: ",L)
return
List=[1,5]
print("List before function call: ",List)
Hitit(List)
print("List after function call: ",List)
List before function call: [1, 5]
This is Inside function now...
List received : [1, 5]
List after adding some elements: [1, 5, 4, 7, 8]
within function after all changes: [1, 4, 7, 8]
List after function call: [1, 4, 7, 8]
def Hitit(L):
print("\t This is Inside function now...")
print("\t List received : ",L)
N=[8,9]
L=N
[Link](6)
print("\t within function after all changes: ",L)
return
List=[1,5]
print("List before function call: ",List)
Hitit(List)
print("List after function call: ",List)
List before function call: [1, 5]
This is Inside function now...
List received : [1, 5]
within function after all changes: [8, 9, 6]
List after function call: [1, 5]
Explanation: The lambda keyword creates an
anonymous function. The x is a parameter, that is
passed to the lambda function. The parameter is
followed by a colon character. The code next to the
colon is the expression that is executed, when the
lambda function is called. The lambda function is
assigned to the z variable.
The lambda function is executed. The number 8 is
passed to the anonymous function and it returns 48 as
the result. Note that z is not a name for this function.
It is only a variable to which the anonymous function
was assigned.
y=6
z = lambda x: x * y
print (z(8))
[Link] writer():
[Link] = 'Sir'
[Link] = (lambda x:title + ' ' + x)
[Link] name
5.
[Link] = writer()
[Link](who('Arthur’))
[Link] writer():
[Link] = 'Sir'
[Link] = (lambda x:title + ' ' + x)
[Link] name
5.
[Link] = writer()
[Link](who('Arthur’))
Sir Arthur
nonlocal keyword
In Python, the nonlocal keyword is used to work with
variables inside nested functions. It allows you to access
and modify a variable in the enclosing scope (the outer
function) from within an inner function.
Flow of Execution:
[Link] outer(a,b):
2. x=a
3. y=b
4. def inner(k,l):
5. t=k+l
6. return t
7. s=inner(x,y)+10
8. return s
[Link] outer2(p):
10. x=p*p
11. z=outer(x,5)
12. return z
[Link](outer2(3)+outer(2,3))
1---9 ----13----9----10----11----1----2----3----4----7----4----
5----6----7----8----11----12----13----1----2----3----4----7----
4----5----6----7----8----13