CHAPTER-2 WORKING WITH FUNCTIONS
Definition
A function is a sub program that acts on data and often returns a value.
Need of a functions
1.Large programs are generally avoided because it is difficult to manage a single
list of instructions.
2. Thus a large program is broken down into smaller units known as functions.
3. A functions is a named unit of a group of programs statements. This unit can be
invoked from other parts of a program.
Advantages of a functions
1.By using a function we make programs handling easier as only a small part of
the program is dealth with a time.
2.it reduce program size.
3.Functions make a program more readable and understandable to a programmer
thereby making program management much easier.
Example of python functions
def sum(a,b): #function header
s=a+b
return s # statement to return computed result
#.........main programs…..
a=int(input(“enter a number”))
b=int(input(“enter another number”))
print(sum(a,b) ) # function call inside prints()
Python function types
1. Built in functions
These are pre-defined functions and are always available for use.
Example are len(),type(),int(),input()
2. Functions defined in modules
These functions are pre-defined in particular modules and can only be
used when the corresponding modules is imported.
Examples are sin(),cos(),sqrt(),
to use above function we have to import math modules.
3. User defined functions
These are defined by the programmer .As a programmers we can create
our own functions.
Creating and defining functions in python
A function in python is defined as following general format.
def<function name>[<parameters>]: #function header
<statement>
[<statement>]
Example 1
def multiply(a,b): #def is a keyword,multiply is name of function
mul=a*b
return mul
Example 2
def greet():
print(“good morning”)
Flow of execution in a function call.
The flow of execution refers to the order in which statements are executed during
a program run.
def fun():
return
#......main…..
func()
print(…..)
Example:-program to add two numbers through a function
1. def calusum(x,y):
2. s=x+y #statement 1
3. return s #statement 2
4.num1=float(input(“enter first number”) ) #statement 3
5.num2=float(input(“enter second number”)) #statement 4
6.sum=calcsum(num1,num2) #statement 5
7.print(“sum of two given number”, sum) #statement 6
Flow of execution.
14561237
Arguments and Parameters
The values being passed through a function-call statement are called
arguments(or actual parameters or actual arguments).the values received in the
function definition/header are called parameters(or formal parameters or formal
arguments).
Example
def multiply(a,b): #a and b are parameters
print(a*b)
y=3
z=4
multiply(y,z) #y and z are arguments passed in function call 1
multiply(y,9) # y and 9 are actual arguments in function call 2
PASSING PARAMETERS
Python supports three types of formal arguments/parameters
1. Positional arguments(required arguments)
2. Default arguments
3. Keyword arguments/named arguments.
Positional arguments
When the function call statements must match the numbers and order of
arguments as defined in the function definition,this is called the positional
argument matching.
Example
def check(a,b,c):
Then possible function calls for this can be
check(x,y,z) #3 values (all variables)passed
check(2,x,y) #3 values(literal+variables) passed
check(2,5,7) #3 values(all literals) passed
Default Arguments
A parameter having default value in the function header is known as a default
parameters.
def multiply(a,b,c=10): # c is the default parameter
mul=a*b*c
return mul
m1=multiply(5,6) #third argument missing
m2=multiply(2,3,4) # no argument missing.
Default parameter is optional in function call. function call may or may not have
value for it.the default values for parameters are considered only if no value is
provided for that parameters in the function call statement.
Keywords arguments/named arguments.
Keyword arguments are named arguments with assigned values being passed in
the function call statement.
def multiply(a,b,c): # c is the default parameter
mul=a*b*c
return mul
m1=multiply(a=10,c=5,b=3)
m2=multiply(c=4,a=5,b=10)
all the above function calls are valid now,even if the order of arguments does not
match the order of parameters as defined in the function header.
Using multiple argument types together.(mixed type argument)
Python allows us to combine multiple types in a function call.
Example.
def mul(a,b,c=12):
mul=a*b*c
print(mul)
mul(10,b=20) # mixed argument type
Returning values from functions
Functions in python may or may not return a value
There are two types of functions in python.
Functions returning some value(non-void functions)
Functions not returning any value.(void functions)
1.Function returning some value(non-void functions)
The function that returns some computed result in terms of a value is known as
non-void function (fruitful function). The computed value is returned using return
statements as per syntax.
return<value>
The value being returned can be one of the following type.
1.literal
2.a variable
3.an expression
Example
return 5 , return 6+4,return a,return a**3, return (a+8**2)/b
Example
def sum(x,y):
s=x+y
return s
result=sum(5,3)
print(“result=”,result)
the returned value of a function should be used in the caller function/program
inside an expression or a statement.
print(sum(3,4))
2. Functions not returning any value (void function)
The functions that perform some action or do some work but do not return
any computed value or final value to the caller are called void
functions(non-fruitful 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
return is a keyword without any value or expression.
Example
1.void function but no return statement
a. def greet():
Print(“hello”)
b.def greet1 (name):
print(“hello”,name)
2.void function with a return statement
a. def quote():
print(“goodness counts”)
return
b.def prinsum(a,b,c):
print(“sum is “,a+b+c)
return
SCOPE OF VARIABLES
Parts of a program within which a name is legal and accessible is called scope of
the name.
Global scope
A global variable is a variable defined in the “main” program (---main—section).
Such variables are said to have global.
Local scope
A local variable is a variable defined with in a function .such variables are said to
have local scope.
Example 1
def calcsum(x,y):
z=x+y # statement 1
return z #statement 2
num1=int(input(“enter first number”))
num2=int(input(“enter second number”))
sum=calcsum(num1,num2)
print(“sum of the given number is”,sum)
here in the above example variable x,y,z are local variable of function
calcsum() .variable num1,num2, sum are global variable.
Example 2
x=5 # global variable
def func(a):
b=a+1 # a,b are local variable
return b
y=input(“enter number”) #global variable
z=y+func(x) #z,global variable
print(z)
Name resolution (resolving scope of a name)
LEGB RULE
L-Local name space
E-Enclosing environment
G-Global environment
B-built in environment
Case-1(variable in global scope but not in local scope)
def calcsum(x,y):
s=x+y
print(num1)
return s
num1=int(input(“enter first number”))
num2=int(input(“enter second number”))
print(“sum is”,calcsum(num1,num2))
variable num1 is a global variable not a local variable.
Case-2 (variable neither in a local scope nor in global scope)
def greet():
Print(“hello”,name)
greet()
This would return error as name is neither in local environment nor in global
environment.
Case-3(some variable name in local scope as well as in global scope)
def state1():
tigers=15 #
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
# This statements will create a local variable with name tigers as it is assignment
statement. it won’t refer to tigers of main program.
Output
95
15
95
Case-4(to make a local variable to global by using global keywords)
def state1():
global tigers #
tigers=15
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
#this is an indication not to create local variable with the name tigers,
rather use global variable tigers.
Output
95
15
15