Class XII (As Per CBSE Board) : Computer Science
Class XII (As Per CBSE Board) : Computer Science
syllabus
2020-21
Chapter 2
Functions
Computer Science
Class XII ( As per CBSE Board)
Visit : python.mykvs.in for regular updates
Function Introduction
A function is a programming block of codes which is
used to perform a single, related task. It only runs
when it is called. We can pass data, known as
parameters, into a function. A function can return data
as a result.
#Function block/
def my_own_function(): definition/creation
print("Hello from a function")
a, b, x, y = 1, 2, 3,4
fun(50, 100) #passing value 50 and 100 in parameter x and y of
function fun()
print(a, b, x, y)
Visit : python.mykvs.in for regular updates
Variable’s Scope in function
#Find the output of below program
def fun(x, y): # argument /parameter x and y
global a
a = 10
x,y = y,x
b = 20
b = 30
c = 30
print(a,b,x,y)
a, b, x, y = 1, 2, 3,4
fun(50, 100) #passing value 50 and 100 in parameter x and y of function fun()
print(a, b, x, y)
OUTPUT :-
10 30 100 50
10 2 3 4
Visit : python.mykvs.in for regular updates
Variable’s Scope in function
Global variables in nested function
def fun1():
x = 100
def fun2():
global x
x = 200
print("Before calling fun2: " + str(x))
print("Calling fun2 now:")
fun2() OUTPUT:
print("After calling fun2: " + str(x)) Before calling fun2: 100
Calling fun2 now:
fun1() After calling fun2: 100
x in main: 200
print("x in main: " + str(x))
Visit : python.mykvs.in for regular updates
Variable’s Scope in function
Non local variable
def fun1():
x = 100
def fun2():
nonlocal x #change it to global or remove this declaration
x = 200
print("Before calling fun2: " + str(x))
print("Calling fun2 now:")
fun2() OUTPUT:
print("After calling fun2: " + str(x)) Before calling fun2: 100
Calling fun2 now:
x=50 After calling fun2: 200
fun1() x in main: 50
print("x in main: " + str(x))
Visit : python.mykvs.in for regular updates
Function
Parameters / Arguments Passing and return value
These are specified after the function name, inside the parentheses. Multiple
parameters are separated by comma.The following example has a function with
two parameters x and y. When the function is called, we pass two values, which
is used inside the function to sum up the values and store in z and then return
the result(z):
def sum(x,y): #x, y are formal arguments
z=x+y
return z #return the value/result
x,y=4,5
r=sum(x,y) #x, y are actual arguments
print(r)
Note :- 1. Function Prototype is declaration of function with name ,argument
and return type. 2. A formal parameter, i.e. a parameter, is in the function
definition. An actual parameter, i.e. an argument, is in a function call.
Visit : python.mykvs.in for regular updates
Function
Function Arguments
Functions can be called using following types of formal arguments −
• Required arguments/Positional parameter - arguments passed in correct positional order
• Keyword arguments - the caller identifies the arguments by the parameter name
• Default arguments - that assumes a default value if a value is not provided to argu.
• Variable-length arguments – pass multiple values with single argument name.
#Required arguments #Keyword arguments
def fun( name, age ):
def square(x): "This prints a passed info into this
z=x*x function"
return z print ("Name: ", name)
print ("Age ", age)
r=square() return;
print(r)
#In above function square() we have to # Now you can call printinfo function
definitely need to pass some value to fun( age=15, name="mohak" )
argument x. # value 15 and mohak is being passed to
relevant argument based on keyword
used for them.
Visit : python.mykvs.in for regular updates
Function
#Default arguments / #Variable length arguments
def sum( *vartuple ):
#Default Parameter s=0
def sum(x=3,y=4): for var in vartuple:
z=x+y s=s+int(var)
return z return s;
E.g.
x = lambda a, b : a * b
print(x(5, 6))
OUTPUT:
30
r="Mohan"
welcome(r) OUTPUT
print(r) Mohan
r=(3, 1) OUTPUT
m=Max(r) 3
print(m)
Visit : python.mykvs.in for regular updates
Pass dictionary to a function
OUTPUT :
2.0
Visit : python.mykvs.in for regular updates
Functions using libraries
Functions available in Python Math Module
OUTPUT:
I love programming
Visit : python.mykvs.in for regular updates
Functions using libraries
String functions:
Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
zfill() Fills the string with a specified number of 0 values at the beginning