Professional Documents
Culture Documents
CS Gr12 Chapter3 2023-24 (1)
CS Gr12 Chapter3 2023-24 (1)
invoked.
• The use of function is one of the means to achieve modularity and reusability.
• Once defined, a function can be called repeatedly from different places of the program without writing
all the codes of that function every time , or it can be called from inside another function, by simply
writing the name of the function and passing the required parameters,
• The programmer can define as many functions as desired while writing the code.
• Modularity is breaking down the larger program to smaller code segments so as to reduce the
• A function defined can be called (invoked) any number of times at any part of the program.
Note: The varname/s in function with return in the above two lines, are the variables where we want to assign
the value/s returned from the function . In case of multiple return values, we need to unpack them providing
• If you do not know how many arguments that will be passed into your function, add
a * before the parameter name in the function definition.
• This way the function will receive a tuple of arguments, and can access the items
accordingly.
#fn definition
Output will be:
def fn(): Inside __main__
print("Hello") Hello
print(‘Inside’, __name__)
Note:
• The function calling another function is called the caller and the function being called is the called
function (or callee).
• In the above code __main__ is the caller of callee fn()
FLOW OF EXECUTION IN A FUNCTION CALL
The flow of execution refers to the order in which statements are executed during a program run.
• Program execution begins with first statement of __main__ segment.
• Comment lines if any are ignored.
• Python just executes the function header line to determine that it is proper function header and ignores all lines
in the function body initially. The statements inside function body are not executed until the function is invoked.
• In python,, a function can define another function inside it. But, since the inner function definition is inside a
function-body, the inner definition isn’t executed until the outer function is called.
• When a code-line contains a function-call, program control first jumps to the function header line and then to the
first line of the function-body and starts executing its statements creating an execution frame for that function.
• A function ends with a return statement or the last statement of the function body, whichever occurs earlier.
• If the called function returns a value (using return statement), then the program control will jump back to the
function call statement and completes it by replacing the function call statement with the return value.
• If the called function doesn’t return any value, then the program control jumps back to the line following the
function call statement.
ARGUMENTS AND PARAMETERS
• Arguments : -Values being passed through function call statement. Also called
actual parameters or actual arguments. They can be literals, variables (containing
value) or expressions.
– Eg :
fn(“Manik”)
fn(10)
n=10
fn(n) - Here “Manik”, 10 or variable “n” are known as actual parameter/argument.
• Parameters : - Values being received in function definition header. Also called
formal parameters or formal arguments. They must be some names or identifiers
only (i,e, variables) to hold incoming values. It cannot have an expression.
– Eg: def fn(s) - In this function header, identifier “s” refers to formal parameter.
– We cannot write definition as: - def fn(a+10) , as parameter cannot be expression
• If you are passing values of immutable types to the called functions, then the called
function cannot alter the values of passed arguments. But, if you are passing values
of mutable types then called function would be able to make changes in them.
• Function call mechanism is of two types: - Mutability and Immutability of
arguments/parameters to a function plays a important role here.
– Call by Value : The called function makes a separate copy of passed parameters and then works with them.
So original values remain unchanged in the __main__ program. Immutable types implement this.
– Call by Reference : The called function works with original arguments passed to it. So any changes made
with them inside the function will take place in original arguments itself. Mutable types implement this.
– Eg: -
Call by Value: Call by Reference:
Note:
• Changes in immutable types are not reflected in the caller function at all.
• Changes in any mutable type of parameter are reflected in caller function if its name is not assigned a
different variable or datatype.
PASSING PARAMETERS
2. Default arguments
• It Provides complete control and flexibility over the values sent as arguments for the corresponding
parameters.
• We can write any argument in any order provided we name the arguments when calling the function.
• Example:
• All the above function calls are valid even if the order of arguments does not match with the order of
parameters defined in the function header.
USING MULTIPLE ARGUMENT TYPES TOGETHER
• We can combine multiple argument types in a function call following the rules stated below:
– An argument list must first contain positional argument followed by any keyword argument and then
default argument.
• Eg: If the function call statements for the function interest() is given as: -
– interest(5000,time=4) # For prin it takes 5000 as positional argument, for time it takes 4 as keyword
argument and for rate it takes 0.10 as default argument.
• Instead if we give:
# Example Program
o/p will be:
#Function to add 2 to a number
def add(x): Adding 2 to 5 and multiplying the result with 2: 14
return x + 2
def fn(a,b): # names a,b and t are local variables o/p is:
t=a+b
return t sum is 8
n1=5 #names n1, n2 and s are global variables
n2=3
s=fn(n1,n2)
print('sum is',s)
Contd….
Local Variable scope
Enclosing Variable scope
Enclosing scope contd…
Note –
The nested function cannot be called from outside that enclosing
function
Global scope
Built_in scope
LEGB rule that
determines the
scope of a variable
O/p
Usage of the keyword global
O/p
NAME RESOLUTION
def fn(a,b):
t=a+b
print('value brought are',n1,n2) #global variable used
return t
n1=5
n2=3
s=fn(n1,n2)
print('sum is',s)
Same variable name in local scope and global scope
i. A function that takes a number as argument and calculates cube for it. The function does not return a
value. If there is no value passed to the function in function call, the function should calculate cube of 2.
ii. A function that takes two char arguments and returns true if both arguments are equal otherwise false.
ii. A function u2(character,count) to display specific character specific number of times in a straight
line,
Answers to assignment questions given earlier
Alternate way to write the previos prg
Chapter 2 Assignment answers -
Write a program that creates a tuple of all the integers between 1 to
100, that are multiples of 3 and 5. Display the tuple
Write a program to create a dictionary to input n number of times the name
and age of a person, where n is accepted from user (for number of people).
Store the details in a Dictionary and at the end decide and display who is the
eldest of them.
O/p should be like:
O/p should be like:
Enter how many people:3
Enter name:Madhu
Enter the age:18
Enter name:Kiran
Enter the age:56
Enter name:Sunil
Enter the age:54
Prg is in next slide
Values stored are:
Madhu -18
Kiran -56
Sunil -54
Eldest person is: Kiran with age 56