Function & Modules IP PDF
Function & Modules IP PDF
FUNCTIONS
&
MODULES
FUNCTIONS & MODULES
Function and Modules makes a part in the program
structure.
Defined in the program to break down the
complexity of a given task .
They perform specific task(sub-task) in a given
task(problem).
They can be reused in [other] program(s)
FUNCTIONS & MODULES
Python program contains three components
Library or Package
Modules
Functions / sub-modules
Library is a collection of various packages.
Package is a directory of modules.
Example
The following function takes a string as input parameter and prints it on standard screen.
def printme( str ):
"This prints a passed string into this function"
print str
return
FUNCTIONS
Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to be included in the
function and structures the blocks of code.
Once the basic structure of a function is finalized, it can be executed by calling it from another
function or directly from the Python prompt.
Following is the example to call printme() function −
#!/usr/bin/python
Output:
I'm first call to user defined function!
Again second call to the same function
FUNCTIONS
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
We can add as many arguments as we want, just separate them with a comma.
The following example has a function with one argument (fname). When the
function is called, we pass along a first name, which is used inside the function
to print the full name:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Output:
Emil Refsnes
Tobias Refsnes
Linus Refsnes
FUNCTIONS
Arguments / Parameters
What is parameter? Is it same as argument?
Yes.
The terms parameter and argument can be used for the same thing:
information that are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the
function definition.
An argument is the value that are sent to the function when it is
called.
Number of Arguments
By default, a function must be called with the correct number of
arguments.
Meaning that if a function expects 2 arguments, then we have to
call the function with 2 arguments, not more, and not less.
FUNCTION ARGUMENTS
A function can be called by using the following types
of formal arguments −
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
FUNCTION ARGUMENTS
Required arguments
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.
To call the function printme(), we definitely need to pass one argument, otherwise it
gives a syntax error as follows −
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
# Now you can call printme function
printme()
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
FUNCTION ARGUMENTS
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 −
#!/usr/bin/python
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
FUNCTION ARGUMENTS
Default arguments
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
Name: miki
Age 50
Name: miki
Age 35
FUNCTION ARGUMENTS
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 −
#!/usr/bin/python
# Function definition is here
Output is:
10
Output is:
70
60
50
FUNCTION ARGUMENTS
The return Statement
The statement return [expression] exits a function, optionally passing back an expression
to the caller. A return statement with no arguments is the same as return None.
All the above examples are not returning any value. You can return a value from a
function as follows −
Syntax: <module_name>.<function_name>
MODULE CONTINUED…
import math
def circle_area(radius):
#pi * r * r
return math.pi * radius * radius
def square_area(side):
return side * side