Computer Science Class-Xii Code No. 083 2021-22: Functions in Python
Computer Science Class-Xii Code No. 083 2021-22: Functions in Python
CLASS-XII
Code No. 083
2021-22
FUNCTIONS IN PYTHON
Topics of Functions:
Types of function (built-in functions, functions
defined in module, user defined functions), creating
user defined function, arguments and parameters,
default parameters, positional parameters, function
returning value(s), flow of execution, scope of a
variable (global scope, local scope)
Functions are the subprograms that perform specific task. Functions are the
small modules.
Types of Functions:
There are three types of functions in python:
1. Library Functions (Built in functions)
2. Functions defined in modules
3. User Defined Functions
1. Library Functions: These functions are already built in the python library.
For example: type( ), len( ), input( ) etc.
import math
S. No. Function Description Example
1 sqrt( ) Returns the square root of a number >>>math.sqrt(49)
7.0
2 ceil( ) Returns the upper integer >>>math.ceil(81.3)
82
3 floor( ) Returns the lower integer >>>math.floor(81.3)
81
4 pow( ) Calculate the power of a number >>>math.pow(2,3)
8.0
5 fabs( ) Returns the absolute value of a number >>>math.fabs(-5.6)
5.6
6 exp( ) Returns the e raised to the power i.e. e3 >>>math.exp(3)
20.085536923187668
randint( ) function generates the random integer values including start and
end values.
Syntax: randint(start, end)
It has two parameters. Both parameters must have integer values.
Example:
import random n=random.randint(3,7)
*The value of n will be 3 to 7.
3 User Defined Functions: The functions those are defined by the user are called
user defined functions.
Where:
Example:
def display(name):
print("Hello " + name + " How are you?")
display(“Ravi”)
output- HelloRaviHow are you?
1.)Positional arguments
2.)Default arguments
3.)Keyword (or named ) arguments
1) Positional arguments
The values of arguments are matched with parameters position wise.
Eg.) If the function header is
def check(a, b, c):
2) Default Arguments
A parameter having default value in the function header is known as a default
parameter.
Eg 2)
If the function header is
def interest(P, T, R =10):
If the function call is
SI = interest (5400,2) #Third argument is missing. Here P gets value
# of 5400, T gets value
#of 2 and R takes the default value 10
If the function call is
SI = interest(6100,3,15) #Here P gets value of 6100,T gets value of 3
#and R gets value of 15
Eg)
def interest (P, R,T ) :
SI = (P*R*T) /100
print(“Simple Interest =”, SI)
interest(P=2000,T=2,R=10)
interest(T=4,P=2600,R=9)
interest(T=2,R=12,P=2000)
In a function call statement:
*An argument list must first contain positional arguments followed by any
keyword arguments.(If positional arguments comes after keyword arguments
then it will result into error)
num1 = int(input (“Enter first number”)) num1 = int(input (“Enter first number”))
num2 = int(input(“Enter second num2 = int(input(“Enter second
number”)) number”))
sum = calsum(num1, num2 ) sum = calsum(num1, num2 )
print(“Sum of 2 numbers=”, sum) print(“Sum of 2 numbers=”, sum)
11 How to use the global variable inside local scope when the same variable
name is having local scope as well as global scope?/ How can you access a
global variable inside the function , if function has a variable with same
name?
tigers = 95
print(tigers)
state1( )
print(tigers)
o/p
95
15
15
When immutable type arguments (eg. Numbers, strings etc) are passed in a
function call then any change in its value will also change the memory
address it is referring to. So after returning from the function definition the
originally passed variable remains unchanged.
a) def main( )
print(“hello”)
b) def func2( ) :
print(2 + 3)
c) def compute( ) :
print(x * x)
d) square( a )
return a*a
14
15
16
17
18
o/p
1000
num1= 1.0 num2= 100
ANSWER
a)Parameter
b)Keyword argument
c)Argument
d)Default parameter
e) Keyword argument
f) Global variable
g) Local variable
20
ANSWER
total = 0
ANSWER
21
ANSWER
i)
1
1
1
ii)
1
10
1
iii)
1
10
10
iv)
Hello there!
22
def addEm(x,y,z):
return x+y+z
print(“the answer is”, x+y+z)
ANSWER
23
ANSWER
i) 5 times 5 =25
ii) 25
24
ANSWER
25
ANSWER
i)
[1,2,3,[4]] [1,2,3,[4]]
ii)
[23,35,47,[49]]
23 35 47 [49]
True
26
27
28
29
30
31
32
33
34
35
37
38
39
40