Professional Documents
Culture Documents
Functions
Chapter 3
IN THIS CHAPTER
Introduction
Understanding Functions
Defining Functions in Python
Flow of Execution in a Function
Call
Passing Parameters
Returning Values From Functions
Composition
Scope of Variables
yth
p
.o.... n
Python
Functions
.o.... n
UNDERSTANDING
FUNCTIONS
A programmers can write their own function(s). These
functions can then be combined to form a module which
can then be used in other programs by importing them.
Note: The fi rst line of function definition, i.e., Line No. 1 is called
header and the rest, i.e. Line No. 2 in our example, is known as bo
dy . Consisting of sequence of indented (4 space) Python stat
# Line No. 1
ement(s), to perform a t ask.
# Line No.2
SOME TERMS
• Function header : the first line begins with keyword def
and ends with a colon (:), specifies the name of function
and its parameters.
• Parameters : variables that are listed within the
parentheses of a function header.
• Indentation: the blank in the beginning of a statement
within a block.
pyth.o. ..
n
.
n
.
Parameters or
ARGUMENTS
n
.
Function arguments
There are various types of User-defined
n
User-defined Functions without
.
argument
Out put :
Enter a number:45
45 is odd
PASSING PARAMETERS
Python supports three types of formal arguments / parameters:
1. Positional arguments (required arguments
2. Default arguments
3. Keywords (or named ) arguments
Default Arguments
A parameter having default value in the function header is
known as a default parameter.
Non default arguments cannot follow default argument .
User-defined Functions with default argument
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
icfl AST
Exception
.
Transfer of value from argument to parameter
with keywords
#order of argument can be change by
mentioning parameter's name = argument for
all arguments
Functions with Variable-
length arguments
You may need to process a fun ct ion for more
arguments than you specified while defining
the funct ion. These arguments are
called variable-lengt h argument s and are not
named in the funct ion defi nit ion, unlike
required and default arguments.
Syntax
def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite return
[expression]
Function return
statement
There are various types of User-defined
statements:
1. Functions without return statement
>>>
x=evenodd(34)
>>> print
x even
Scope of a variables
Variables can only reach the area in which they are
defined, which is called scope.
Think of it as the area of code where variables can be
used.
There are two types of variable scopes :
->L, pe
L ocal Scope
Global Scope n
.
eg:
X=50
V I . \ J ... ( ) •.
1 ns1de test x is ", x)
print("value of x is ", x)
Output:
inside test x is 50
value of x is 50
pyth.o.
..
Local Scope n
.
v\
X=50
de f test ( ):
global x =5
y=2
print('value of x & y inside the function are ' , x , y)
Print('value of x outside function is ' ', x' )
def main():
print ("Hello
World") if _ name_ =="_
main_ ":
main()
Questions on Functions yth
p
Q l . Write a program to find a given number is prime
or not using functions. .o.... n
>>> evenodd(45)
45 is odd
>>> evenodd(66)
66 is even
pyth.o.
Questions on Functions ..
n
.
Q2. Write a program to find whether a given number
is even or odd using functions.
>>> def prime(a):
f=O
i in range(l,a):
ror if (a%i == O):
f=f
+l if ( f>
l) :
print a," is not prime number"
else:
print a," is prime number"
>>> prime(2)
2 is prime number
>>> prime(4)
pyth.o.
Questions on ..
n
Functions
Q3. Write a program to find factors of a given
.
>>> e prime(a):
print " factors
are :", for i in
range(l,a+l):
if(a%i==0):
print i,
>>> prime(lO)
factors are : 1 2 5 10
.
Questions on
Functions
QS. Write a Python function to find the Max of
three numbers.
def max_of_tw(o x, y ):
f X y:
retJ x
return y
def max_of_1e( x, y, z ):
retu max_of_two( x, max_of_two(
y, max_of_three(3,
prin z ) ) 6,
-5))
.
Questions on Functions
def s m( numbers):
total 0
for x in numbers:
total
+= x
return
total
print(sum(
(8, 2, 3, 0,
7)))
.
Questions on Functions
Q7. Write a Python program to
reverse a st rin g.
def string_rever(strl):
rstrl = ' '
index len( strl) wh
index > 0:
rstrl += strl[ index - 1 ]
index index 1
etu rstrl
pri1t(string_rever(s'e123a4bcd'))