2025 2026 Class XII Computer Science Chapter 2 AW
2025 2026 Class XII Computer Science Chapter 2 AW
• Variable-length 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, you definitely need to pass one argument,
otherwise it gives a syntax error as follows –
def printme( str ):
"This prints a passed string into this function"
print( str)
# Now you can call printme function
printme()
When the above code is executed, it produces the following result:
Traceback (most recent call last): File "test.py", line 11, in <module>
printme();
TypeError: printme() takes exactly 1 argument (0 given)
Keyword arguments (Not in syllabus)
Keyword arguments are related to the function calls. When you use
keyword arguments in a function call, the caller identifies the arguments
by the parameter name.
This allows you to skip arguments or place them out of order because the
Python interpreter is able to use the keywords provided to match the values with
parameters. You can also make keyword calls to the printme function in the
following ways –
# Function definition is here
def printme( str ):
#"This prints a passed string into this function"
print( str)
# Now you can call printme function
printme( str = "My string")
The following example gives more clear picture. Note that the order of
parameters does not matter.
def printinfo( name, age ):
print ("Name: ", name)
print ("Age ", age )
printinfo( age=50, name="miki" )
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 –
def printinfo( name, age = 35 ):
print("Name",name);
print( "Age ", age)
printinfo( age=50, name="miki" )
printinfo( name="miki" )
Note : default arguments should follow non-default arguments. Failing to do so
results in syntax error as shown in the example below :
def multiplication(num1=2,num2):
return (num1*num2)
# here first argument is passed as positional arguments while other two as keyword argum
ent
my_func(12, b=13, c=14)
# same as above
my_func(12, c=13, b=14)
# this is wrong as positional argument must appear before any keyword argument
# my_func(12, b=13, 14)
fun(3) # function 1
fun(3,7,10) # function2
fun(25,c=20) # function 3
fun(c=20,a=10) # function 4
#fun(29,b=10,20) # function 5 error positional argument follows keyword argument
#fun(37,a=47) # function 6 error multiple values assign for a
# fun(d=56) # function 7 error unexpected keyword argument d
fun(b=56) # function 8 error required argument to a is missing correction fun(56) or
fun(a=56)
'''
This way the function will receive a tuple of arguments, and can access the items
accordingly:
# example 1 of variable length arguments
def add(*N):
for I in N:
print(I,end=' ')
print()
add(10,20,30,40)
add(2,4)
add()
# example 2 of variable length arguments
def my_function(*kids):
print("The youngest child is " + kids[2])
We can return multiple values from function using the return statement by separating them
with a comma (,). Multiple values are returned as tuples.
def bigger(a, b):
if a > b:
return a, b
else:
return b, a
s = bigger(12, 100)
print(s)
print(type(s))
Expected Output:
(100, 12)
<class 'tuple'>
Scope of Variables
All variables in a program may not be accessible at all locations in that
program. This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can
access a particular identifier. There are two basic scopes of variables in
Python −
Global variables Local variables
Example 1:
global_var = 12 # a global variable
def func():
local_var = 100 # this is local variable
print(global_var) # you can access global variables in side function
#print(local_var) # you can't access local_var outside the function, because as soon as
function ends local_var is destroyed
Example 2:
xy = 100
def cool():
xy = 200 # xy inside the function is totally different from xy outside the function
print(xy) # this will print local xy variable i.e 200
cool()
You can bind local variable in the global scope by using the global keyword followed by
the names of variables separated by comma (,).
Example 3:
t=1
def increment():
global t # now t inside the function is same as t outside the function
t=t+1
print(t) # Displays 2
increment()
print(t) # Displays 2
Note that you can't assign a value to variable while declaring them global .
Example 4:
t=1
def increment():
#global t = 1 # this is error
global t
t = 100 # this is okay
t=t+1
print(t) # Displays 101
increment()
print(t) # Displays 101
Note: In fact there is no need to declare global variables outside the function. You can
declare them global inside the function.
Example 5:
def foo():
global x # x is declared as global so it is available outside the function
x = 100
foo()
print(x)
Note:
• Any modification to global variable is permanent and affects all the functions where it is
used.
• If a variable with the same name as the global variable is defined inside a function, then
it is considered local to that function and hides the global variable.
• If the modified value of a global variable is to be used outside the function, then the
keyword global should be prefixed to the variable name in the function
Flow of Execution :
Flow of execution can be defined as the order in which the statements in a program are
executed. The Python interpreter starts executing the instructions in a program from the
first statement. The statements are executed one by one, in the order of appearance from
top to bottom. When the interpreter encounters a function definition, the statements inside
the function are not executed until the function is called. Later, when the interpreter
encounters a function call, there is a little deviation in the flow of execution. In that case,
instead of going to the next statement, the control jumps to the called function and executes
the statement of that function. After that, the control comes back the point of function call
so that the remaining statements in the program can be executed. Therefore, when we read
a program, we should not simply read from top to bottom. Instead, we should follow the
flow of control or execution. It is also important to note that a function must be defined
before its call within a program.
Example 1:
def Greetings(Name):
print("Hello "+Name)
Greetings("John")
print("Thanks")
Example 2:
def RectangleArea(l,b):
return l*b
l = input("Length: ")
b = input("Breadth: ")
Area = RectangleArea(l,b)
print(Area)
print("thanks")
Example 3:
helloPython() #Function Call
def helloPython(): #Function definition
print("I love Programming")
The error ‘function not defined’ is produced even though the function has been defined.
When a function call is encountered, the control has to jump to the function definition and
execute it. In the above program, since the function call precedes the function definition,
the interpreter does not find the function definition and hence an error is raised.
That is why, the function definition should be made before the function call as shown
below:
def helloPython(): #Function definition
print("I love Programming")
helloPython() #Function Call
5.