PYTHON FUNCTIONSοΏ½οΏ½οΏ½οΏ½οΏ½Submitted By: Ms. Rani Chandi
Python Functions
Defining a Function
Here are simple rules to define a function in Python:
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior, and you need to inform them in the same order that they were defined.
def printme( str ):
"This prints a passed string function"
print str
return
Calling a Function
def printme( str ): "This is a print functionβ
print str;
return;
printme("I'm first call to user defined function!");
printme("Again second call to the same function");
I'm first call to user defined function!
Again second call to the same function
Pass by reference vs value
All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example:
def changeme( mylist ): "This changes a passed listβ
mylist.append([1,2,3,4]);
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
There is one more example where argument is being passed by reference but inside the function, but the reference is being over-written.
def changeme( mylist ): "This changes a passed list"
mylist = [1,2,3,4];
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
Values inside the function: [1, 2, 3, 4]
Values outside the function: [10, 20, 30]
Function Arguments:
A function by using the following types of formal arguments::
Required arguments:
def printme( str ): "This prints a passed string"
print str;
return;
printme();
Traceback (most recent call last):
File "test.py", line 11, in <module> printme();
TypeError: printme() takes exactly 1 argument (0 given)
Keyword arguments:
def printme( str ): "This prints a passed string"
print str;
return;
printme( str = "My string");
My string
Following example gives more clear picture. Note, here order of the parameter does not matter:
def printinfo( name, age ): "Test function"
print "Name: ", name;
print "Age ", age;
return;
printinfo( age=50, name="miki" );
Name: miki Age 50
Default arguments:
def printinfo( name, age = 35 ): βTest function"
print "Name: ", name;
print "Age ", age;
return;
printinfo( age=50, name="miki" );
printinfo( name="miki" );
Name: miki Age 50 Name: miki Age 35
Variable-length arguments:
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
def printinfo( arg1, *vartuple ):
"This is test"
print "Output is: "
print arg1
for var in vartuple:
print var
return;
printinfo( 10 );
printinfo( 70, 60, 50 );
Output is:
10
Output is:
70
60
50
The Anonymous Functions:
You can use the lambda keyword to create small anonymous functions. These functions are called anonymous because they are not declared in the standard manner by using the def keyword.
lambda [arg1 [,arg2,.....argn]]:expression
Example:
sum = lambda arg1, arg2: arg1 + arg2;
print "Value of total : ", sum( 10, 20 )
print "Value of total : ", sum( 20, 20 )
Value of total : 30
Value of total : 40
Scope of Variables:
Global variables
Local variables
total = 0; # This is global variable.
def sum( arg1, arg2 ):
"Add both the parameters"
total = arg1 + arg2;
print "Inside the function local total : ", total
return total;
# Now you can call sum function
sum( 10, 20 );
print "Outside the function global total : ", total
Inside the function local total : 30
Outside the function global total : 0
THANK οΏ½ YOU