Chapter 1
Working with Functions
Function-
A function is subprogram that acts on data and often returns a value.
A large program is broken down into smaller units known as function. A function is a named unit of a
group of program statements. This unit can be invoked from any part of the program.
A function reduces the size of the program. Functions make a program more readable and
understandable thereby making program management much easier.
A function is defined as per the following syntax:
def functionname([parameter list]):
Statement1
Statement2
Statement2
:
:
Various elements in a function definition are-
Calling/Invoking a function –
A function is called or invoked by mentioning its name along with the arguments. General syntax of
calling a function is
Functionname( arguments-to-passed)
For example-
def multiply(x,y):
c=x*y
print (c)
a=5
b=4
multiply(a,b)
Python functions-
Python functions are of three types-
Example of user defined function-
def sum(x,y):
c=x+y
return c
z=sum(2,3)
print (z)
Argument and Parameter
Values being passed (through function call) are called Arguments and values being received (in function
definition) are called parameters.
Arguments in Python can be one of these value types:
a. literais
b. variables
c. expressions
The alternative names for argument are actual parameter and actual argument. Alternative names for
parameter are formal parameter and formal [Link] for a function as defined below:
def multiply(a, b) :
print (a * b)
The following are some valid function call statements:
multiply( 3, 4) # both literal arguments and one variable argument
multiply( p, 5) # one literal and formal parameters or formal
multiply(p , p+1) # one variable and one expression argument
Passing Parameter
Python supports three types of formal argument/Parameters :
1. Positional arguments (Required arguments)
2. Default arguments
3. Keyword (or Named) arguments
1. Positional/Required Arguments-
In Positional or Required Arguments the number of passed values (arguments) is equal to the number
of received values (parameters).
For example-
def sub(x,y):
c=x-y
return c
a=4
b=3
z=sub(a,b)
print(z)
2. Default Arguments-
Python allows you to assign default value(s) to a function’s parameter(s) which is used when
matching argument is not passed in the function call statement. The default values are
specified in the function header of function definition. Following is an example of function
with default values :
def sum(x, y=6, z=2):
c=x+y+z
return c
m=sum(3)
print (m)
In a function header, any parameter cannot have a default value unless all parameters appearing on
its right have their default values.
3. Keyword (Named) Arguments-
In Keyword or named argument you to write any argument in any order provided you name the
arguments when calling the function, as shown below:
def interest(princ , time, rate):
inter=(princ*time*rate)/100
return inter
I=interest(time=4, princ=5000, rate=0.2)
print(I)
Returning values from functions-
Functions in Python may or may not return a value. There can be broadly two types of functions in
Python:
1. Functions returning some value (non-void functions)
2. Functions not returning any value (void functions)
1. Functions returning some value (Non-void functions or fruitful functions)
The functions that return some computed result in terms of a value are called Non-void functions. The
computed value is returned using return statement as per syntax:
return value
The value being returned can be one of the following:
a. a literal b. a varIable c. an expression
For example, following are some legal return statements :
return 5 # literal being returned
return 6+4 # expression involving literais being returned
return a # variable being returned
return a**3 # expression involving a variable and literal, being returned
return (a + 8**2) / b # expression involving variables and literais, being returned
return a + b/c # expression involving variables being returned
For example-
def sum (x, y) :
s=x+y
return s
result = sum (5, 3)
print(result)
2. Functions not returning any value (Void functions or non-fruitful functions)
The function that perform some action or do some work but do not return any computed
value to the caller are called void function. A void function may or may not have a return statement.
If a void function has a return statement, then it takes the following form:
return
that is, keyword return without any value or expression.
Following is an example of void function
def greet( )
print(”helloz”)
def quote( )
print(”Goodfless counts! !“)
return
greet()
quote()
The void functions do not return a value but they return a legal empty value of Python i.e. ,
None. Every void function returns value None to its caller. So if need arises you can assign this
return value somewhere as per your needs, e.g., consider following program code:
def greet( ):
print (“helloz”)
a=greet()
print(a)
the above program will give output as:
helloz
None
Returning Multiple Values-
Python allows you to return more than one value from a function.
The return statement inside a function body should be of the form given below:
return value1, value2, value3,……
The function call statement should receive or use the returned values in one of the following ways
(a) Either receive the returned values in form a tuple variable, i.e., as shown below:
def squared(x, y, z):
return x*x, y*y, z*z
t = squared(2,3,4)
print(t)
output will be-
(4, 9, 16)
(b) Or you can directly unpack the received values of tuple by specifying the same
number of variables on the left-hand side of the assignment in function call, e.g.,
def squared(x, y, z):
return x * x, y * y, z * z
v1,v2, v3 = squared(2,3,4)
print(v1,v2,v3)
output will be-
4 9 16
Scope of a Variable
Parts of a program within which a variable is accessible is called scope of the variable.
There are two kinds of scopes in Python
Global Scope-A variable declared outside the body of all functions and blocks is said to have global
scope i.e. it can be used inside the whole program and all the functions and blocks contained within
the program and that variable is called global variable.
Local Scope- A variable declared in a function body is said to have local scope i.e. it can be used only
within this function and the other blocks contained under it and that variable is called local variable.
For example:
def sum(x,y):
c=x+y
return c
num1=int(input(“enter first number”))
num2=int(input(“enter second number”))
z=sum(num1,num2)
print (z)
in the above program num1 and num2 are global variables and x, y and c are local variables.
def state():
tiger=15
print (tiger)
tiger=95
print(tiger)
state()
print(tiger)
output will be_
95
15
95
in the above program local variable created with the same name as that of the global variable, it hides
the global variable. As in the above code local variable tiger hides the global variable tiger in function
state()
If you want to use the value of already created global variable inside a local function without
modifying it, then simply use it .
but if you want to assign some value to the global variable without creating any local variable, then
what to do? This is because, If you assign any value to a variable, Python will create a local variable by
the same name, for this kind of problem Python makes available global statement.
To tell a function that for a particular name, do not create a local variable but use global variable
instead, you need to write :
global variablename
for example:
def state():
global tiger
tiger=15
print (tiger)
tiger=95
print(tiger)
state()
print(tiger)
output will be_
95
15
15