Functions
Functions
CBSE SYLLABUS
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)
1. Built-in Functions
These are pre-defined functions in Python that can be used directly without any import.
Examples:
👉 Common Built-in Functions: print(), len(), sum(), max(), min(), abs(), type(), input(), etc.
Examples:
Page 0 of 28
👉 Common Modules with Functions:
math → sqrt(), factorial(), pow(), ceil(), floor()
random → randint(), choice(), shuffle()
datetime → datetime.now(), strftime()
3. User-defined Functions
These are functions created by the user using the def keyword.
Examples:
Output:
Hello, Welcome to Python!
result = add(5, 3)
print("Sum:", result)
Output:
Sum: 8
Output:
Hello, Guest
Hello, Riya
Output:
Page 1 of 28
Addition: 12
Subtraction: 8
Multiplication: 20
Division: 5.0
Example:
def greet():
print("Hello, Welcome to Python!")
Output:
Hello, Welcome to Python!
Example:
def add(a, b): # a and b are parameters
return a + b
Output:
Sum: 8
3. Default Parameters
Output:
Hello, Guest
Page 2 of 28
Hello, Riya
4. Positional Parameters
Example:
def person_details(name, age):
print("Name:", name)
print("Age:", age)
Output:
Square: 25
Output:
Addition: 12
Page 3 of 28
Subtraction: 8
Multiplication: 20
Division: 5.0
Example:
def greet():
print("Step 1: Inside function")
return "Hello!"
print("Step 2: This will not execute") # Unreachable
Output:
Step 3: Before function call
Step 1: Inside function
Step 4: After function call Hello!
👉 Note: Statements after return do not execute.
3. Scope of a Variable
The scope of a variable determines where it can be accessed.
Example:
def my_function():
x = 10 # Local variable
print("Inside function:", x)
my_function()
print("Outside function:", x) # Error! x is not accessible here
Error:
NameError: name 'x' is not defined
Example:
x = 100 # Global variable
def my_function():
print("Inside function:", x)
Page 4 of 28
my_function()
print("Outside function:", x) # Accessible everywhere
Output:
Inside function: 100
Outside function: 100
Example:
x = 10 # Global variable
def change_value():
global x
x = 20 # Modifying global variable
print("Inside function:", x)
change_value()
print("Outside function:", x) # Modified globally
Output:
Inside function: 20
Outside function: 20
Page 5 of 28
4. an array
Question 5
Pick one of the following statements to correctly complete the function body in the given
code snippet.
def f(number):
#Missing function body
print(f(5))
1. return "number"
2. print(number)
3. print("number")
4. return number
Question 6
Which of the following function headers is correct?
1. def f(a = 1, b):
2. def f(a = 1, b, c = 2):
3. def f(a = 1, b = 1, c = 2):
4. def f(a = 1, b = 1, c = 2, d):
Question 7
Which of the following statements is not true for parameter passing to functions?
1. You can pass positional arguments in any order.
2. You can pass keyword arguments in any order.
3. You can call a function with positional and keyword arguments.
4. Positional arguments must be before keyword arguments in a function call.
Question 8
Which of the following is not correct in the context of Positional and Default parameters in
Python functions?
1. Default parameters must occur to the right of Positional parameters.
2. Positional parameters must occur to the right of Default parameters.
3. Positional parameters must occur to the left of Default parameters.
4. All parameters to the right of a Default parameter must also have Default values.
Question 9
Which of the following is not correct in the context of the scope of variables?
1. Global keyword is used to change the value of a global variable in a local scope.
2. Local keyword is used to change the value of a local variable in a global scope.
3. Global variables can be accessed without using the global keyword in a local scope.
4. Local variables cannot be used outside their scope.
Question 10
Which of the following function calls can be used to invoke the below function definition?
def test(a, b, c, d):
1. test(1, 2, 3, 4)
2. test(a = 1, 2, 3, 4)
3. test(a = 1, b = 2, c = 3, 4)
4. test(a = 1, b = 2, c = 3, d = 4)
Question 11
Which of the following function calls will cause an error while invoking the below function
definition?
def test(a, b, c, d):
1. test(1, 2, 3, 4)
2. test(a = 1, 2, 3, 4)
3. test(a = 1, b = 2, c = 3, 4)
4. test(a = 1, b = 2, c = 3, d = 4)
Question 12
For a function header as follows:
Page 6 of 28
def Calc(X, Y = 20):
Which of the following function calls will give an error?
1. Calc(15, 25)
2. Calc(X = 15, Y = 25)
3. Calc(Y = 25)
4. Calc(X = 25)
Question 13
What is a variable defined outside all the functions referred to as?
1. A static variable
2. A global variable
3. A local variable
4. An automatic variable
Question 14
What is a variable defined inside a function referred to as?
1. A static variable
2. A global variable
3. A local variable
4. An automatic variable
Question 15
Carefully observe the code and give the answer.
def function1(a):
a = a + '1'
a=a*2
function1("hello")
1. Indentation Error
2. Cannot perform mathematical operation on strings
3. hello2
4. hello2hello2
Question 16
What is the result of this code?
def print_double(x):
print(2 ** x)
print_double(3)
1. 8
2. 6
3. 4
4. 10
Question 17
What is the order of resolving the scope of a name in a Python program?
1. B G E L
2. L E G B
3. G E B L
4. L B E G
Question 18
Which of the given argument types can be skipped from a function call?
1. Positional arguments
2. Keyword arguments
3. Named arguments
4. Default arguments
Page 7 of 28
Question 1
A function is a subprogram that acts on data and often returns a value.
Question 2
Python names the top-level segment (main program) as main.
Question 3
In Python, program execution begins with the first statement of the main segment.
Question 4
The values being passed through a function-call statement are called arguments.
Question 5
The values received in the function definition/header are called parameters.
Question 6
A parameter having a default value in the function header is known as a default parameter.
Question 7
A default argument can be skipped in the function call statement.
Question 8
Keyword arguments are the named arguments with assigned values being passed in the
function call statement.
Question 9
A void function also returns a None value to its caller.
Question 10
By default, Python names the segment with top-level statements (main program) as main.
Question 11
The flow of execution refers to the order in which statements are executed during a program
run.
Question 12
The default value for a parameter is defined in the function header.
True/False Questions
Question 1
Non-default arguments can be placed before or
False – Non-default arguments must be placed before default arguments in a function
header.
Question 2
A parameter having default value in the function header is known as a default parameter.
Question 3
The first line of function definition that begins with keyword def and ends with a colon (:), is
also known as function header.
Question 4
Page 8 of 28
Variables that are listed within the parentheses of a function header are called function
variables.
False – Variables listed within the parentheses of a function header are called parameters,
not function variables.
Question 5
In Python, the program execution begins with first statement of __main__ segment.
Question 6
Default parameters cannot be skipped in function call.
False – Default parameters can be skipped in a function call because they already have
assigned default values.
Question 7
The default values for parameters are considered only if no value is provided for that
parameter in the function call statement.
Question 8
A Python function may return multiple values.
Question 9
A void function also returns a value i.e., None to its caller.
Question 10
Variables defined inside functions can have global scope.
Question 11
A local variable having the same name as that of a global variable, hides the global variable
in its function.
False – Variables defined inside functions have local scope and cannot be accessed
globally unless declared using the global keyword.
Question 2
Assertion. Non-default arguments cannot follow default arguments in a function call.
Reason. A function call can have different types of arguments.
Question 3
Assertion. A parameter having a default in function header becomes optional in function
call.
Reason. A function call may or may not have values for default arguments.
Question 4
Assertion. A variable declared inside a function cannot be used outside it.
Reason. A variable created inside a function has a function scope.
Question 5
Assertion. A variable not declared inside a function can still be used inside the function if it
is declared at a higher scope level.
Page 9 of 28
Reason. Python resolves a name using LEGB rule where it checks in Local (L), Enclosing
(E), Global (G) and Built- in scopes (B), in the given order.
Question 6
Assertion. A parameter having a default value in the function header is known as a default
parameter.
Reason. The default values for parameters are considered only if no value is provided for
that parameter in the function call statement.
Question 7
Assertion. A function declaring a variable having the same name as a global variable,
cannot use that global variable.
Reason. A local variable having the same name as a global variable hides the global variable
in its function.
Question 8
Assertion. If the arguments in a function call statement match the number and order of
arguments as defined in the function definition, such arguments are called positional
arguments.
Reason. During a function call, the argument list first contains default argument(s) followed
positional argument(s).
Answer-
Question 1
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 2
(c) The assertion is correct but the reason is incorrect.
Question 3
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 4
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 5
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 6
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 7
(a) Both assertion and reason are correct and the reason is a correct explanation of the
assertion.
Question 8
(c) The assertion is correct but the reason is incorrect.
Page 10 of 28
Using Python Libraries
Question 1
A .py file containing constants/variables, classes, functions etc. related to a particular task and
can be used in other programs is called
1. module
2. library
3. classes
4. documentation
Question 2
The collection of modules and packages that together cater to a specific type of applications or
requirements, is called ...............
1. module
2. library
3. classes
4. documentation
Question 3
An independent triple quoted string given inside a module, containing documentation related
information is a ...............
1. Documentation string
2. docstring
3. dstring
4. stringdoc
Question 4
The help <module> statement displays ............... from a module.
1. constants
2. functions
3. classes
4. docstrings
Question 5
Which command(s) modifies the current namespace with the imported object name ?
1. import <module>
2. import <module1>, <module2>
3. from <module> import <object>
4. from <module> import *
Question 6
Which command(s) creates a separate namespace for each of the imported module ?
1. import <module>
2. import <module1>, <module2>
3. from <module> import <object>
4. from <module> import *
Question 7
Which of the following random module functions generates a floating point number ?
1. random()
2. randint()
Page 11 of 28
3. uniform()
4. all of these
Question 8
Which of the following random module functions generates an integer ?
1. random()
2. randint()
3. uniform()
4. all of these
Question 9
Which file must be a part of a folder to be used as a Python package ?
1. package.py
2. __init__.py
3. __package__.py
4. __module__.py
Question 10
A python module has ............... extension.
1. .mod
2. .imp
3. .py
4. .mpy
Question 11
Which of the following is not a function/method of the random module in Python ?
1. randfloat()
2. randint()
3. random()
4. randrange()
Question 1
The file __init__.py must be the part of the folder holding library files and other definitions
in order to be treated as an importable package.
Question 2
A library refers to a collection of modules that together cater to a specific type of needs or
applications.
Question 3
A Python module is a file (.py file) containing variables, class definitions, statements, and
functions related to a particular task.
Question 4
The uniform() function is part of the random module.
Question 5
The capwords() function is part of the string module.
Page 12 of 28
Question 1
A Python program and a Python module mean the same.
Answer: False
Question 2
A Python program and a Python module have the same .py file extension.
Answer: True
Question 3
The import <module> statement imports everything in the current namespace of the Python
program.
Answer: False
Question 4
Any folder having .py files is a Python package.
Answer: False
Question 5
A folder having .py files along with a special file i.e., __init__.py in it is an importable
Python package.
Answer: True
Question 6
The statement from <module> import <objects> is used to import a module in full.
Answer: False
Question 1
Assertion: The documentation for a Python module should be written in triple-quoted
strings.
Reason: The docstrings are triple-quoted strings in Python that are displayed as
documentation when help(<module>) command is issued.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of
the assertion.
Question 2
Assertion: After importing a module through import <module> statement, all its function
definitions, variables, constants, etc., are made available in the program.
Reason: Imported module's definitions do not become part of the program's namespace if
imported through an import <module> statement.
Answer: (c) The assertion is correct, but the reason is incorrect.
Question 3
Assertion: If an item is imported through from <module> import <item> statement, then
you do not use the module name along with the imported item.
Reason: The from <module> import command modifies the namespace of the program and
Page 13 of 28
adds the imported item to it.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of
the assertion.
Question 4
Assertion: Python's built-in functions, which are part of the standard Python library, can
directly be used without specifying their module name.
Reason: Python's standard library's built-in functions are made available by default in the
namespace of a program.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of
the assertion.
Question 5
Assertion: Python offers two statements to import items into the current program: import
<module> and from <module> import <item> , which work identically.
Reason: Both import <module> and from <module> import <item> bring the imported
items into the current program.
Answer: (b) Both assertion and reason are correct, but the reason is not a correct explanation
of the assertion.
Answer-
total = 0 # Global variable
Error
Page 14 of 28
2. What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
Answer-
def Tot(Number): # Method to find Total
Sum = 0
for C in range(1, Number + 1):
Sum += C
return Sum
Error-
1. There should be a colon (:) at the end of the function definition line to indicate the
start of the function block.
2. Python's built- in function for generating sequences is range(), not Range().
3. Python keywords like return should be in lowercase.
4. When calling a function in python, the arguments passed to the function should be
enclosed inside parentheses () not square brackets [].
Answer-
300 @ 200
300 @ 100
120 @ 100
300 @ 120
Page 15 of 28
4. What will the following function return ?
def addEm(x, y, z):
print(x + y + z)
Answer-
ANSWER
The function addEm will return None. The provided function addEm takes three parameters x,
y, and z, calculates their sum, and then prints the result. However, it doesn't explicitly return
any value. In python, when a function doesn't have a return statement, it implicitly
returns None. Therefore, the function addEm will return None.
1
10
1
ANSWER
Hellothere!
Page 16 of 28
print("y =", y, "a =", a)
print(myfunc())
print("y =", y, "a =", a)
ANSWER
y= 5 a= 10
y= 10 a= 2
a+y 12
12
y= 5 a= 10
ANSWER
15
ANSWER
25*50!100!25
Page 17 of 28
10. What will be the output of the following Python code?
def cube(x):
return x * x * x
x = cube(3)
print x
A. 9
B. 3
C. 27
D. 30
11. Which of the following items are present in the function header?
a) function name
b) parameter list
c) return value
d) Both A and B
Page 18 of 28
16. Which of the following function headers is correct?
a) def fun(x=1,y)
b) def fun(x=1,y,z=2)
c) def fun(x=1,y=1,z=2)
d) def fun(x=1,y=1,z=2,w)
Answer-
x is now 50
18. Choose the correct output from the options given below.
print(‘Welcome!’)
print(‘Iam’, name ) # 'name' is undefined
a) Welcome!
b) Error
Iam main
c) Welcome!
d) None of these
Iam name
x = 20
update()
print("x=", x)
a) x=20
b) x=25 x=25 x=25
c) x=20
d) x=25 x=25 x=20
ANSWER
Output
Page 19 of 28
15
13 22
display()
display(5, 1)
display(9)
Answer -
55
63
12 5
b = [1, 2, 3, 4]
c = check(b)
print(c)
Answer-
[6, 7, 8, 9]
a = 20
b = 30
a = abc(a, b)
print(a, b)
b = abc(a)
print(a, b)
a = abc(b)
print(a, b)
Page 20 of 28
Anwer-
ANSWER
50 30
def Output(B=2):
for T in range(0, B):
print(Execute(T), "*", end="")
print()
Output(4)
Output()
Output(3)
Answer-
22# 40# 9# 13#
OldText = "pOwERALone"
ChangeIt(OldText, "%")
Ans: c) PPW%RRllN%
Page 21 of 28
m = m + str[i].lower()
elif str[i].islower():
m = m + str[i].upper()
else:
if i % 2 == 0:
m = m + str[i - 1]
else:
m = m + "@"
print(m.swapcase())
disp('StudyBag$2021')
Answer-
StudyBagG$2$2
add(6, 6)
print(total)
Answer-
12
28. Find and write the output of the following Python code:
def makenew(mystr):
newstr = " "
count = 0
for i in mystr:
if count % 2 == 0:
newstr = newstr + i.lower()
else:
if i.islower():
newstr = newstr + i.upper()
else:
newstr = newstr + i
count += 1
newstr = newstr + mystr[:3]
print("The new string is:", newstr)
makenew("cbseEXAMs@2022")
Answer-
cBsEeXaMs@2022cbs
30. Consider the following code and choose the correct answer:
def nameage(name, age):
return [age, name]
t = nameage('kishan', 20)
print(type(t))
a) 13 b) 25 c) 26 d) Error
32. Consider the code given below and identify how many times the message "Hello" is
printed:
def prog(name):
for x in name:
if x.isalpha():
print('Alphabet')
elif x.isdigit():
print('Digit')
elif x.isupper():
print('Capital Letter')
else:
print('Hello All')
prog('vishal123@gmail.com')
a) 0 b) 2 c) d) 3
Page 23 of 28
print(m)
fun('school2@com')
Answer
SCHOOLbbbbCOM
34.
def Change(P, Q=30):
P=P+Q
Q=P-Q
print(P, "#", Q)
return P
R = 150
S = 100
R = Change(R, S)
print(R, "#", S)
S = Change(S)
Answer
35.
Answer
22 # 40 # 9 # 13 #
36.
def check(n1=1, n2=2):
n1 = n1 + n2
n2 += 1
print(n1, n2)
check() # Call 1
check(2, 1) # Call 2
check(3) # Call 3
Page 24 of 28
Answer-
333253
37.
def check():
global num # Declaring 'num' as a global variable
num = 1000
print(num)
num = 100
print(num)
check()
print(num)
Answer-
1000 100 100
38.
def Total(Number=10):
Sum = 0
for C in range(1, Number + 1):
if C % 2 == 0:
continue
Sum += C
return Sum
print(Total(4))
print(Total(7))
print(Total())
Answer
4 16 25
39.
X = 100 # Global variable
Change()
Change(18, 50)
Change(30, 100)
Answer-
10 # 25 $ 185
18 # 50 $ 318
30 # 100 $ 480
Page 25 of 28
39.
def Updater(A, B=5):
A = A // B
B=A%B
print(A, '$', B)
return A + B
A = 100
B = 30
A = Updater(A, B)
print(A, '#', B)
B = Updater(B)
print(A, '#', B)
A = Updater(A)
print(A, '$', B)
Answer-
3$3
6 # 30
6$1
6#7
1$1
2$7
40.
def Fun1(num1):
num1 *= 2
num1 = Fun2(num1)
return num1
def Fun2(num1):
num1 = num1 // 2
return num1
n = 120
n = Fun1(n)
print(n)
Answer-
120
41.
def Fun1(mylist):
for i in range(len(mylist)):
if mylist[i] % 2 == 0:
mylist[i] /= 2 # Halve even numbers
else:
mylist[i] *= 2 # Double odd numbers
Page 26 of 28
Answer-
[42, 10.0, 3.0, 14, 18, 9.0, 50.0, 25.0, 26]
42.
b = 100 # Global variable
def test(a):
global b # Declare b as global to modify it inside the function
b =b+ a
print(a, b)
test(10)
print(b)
Answer-
10 110
110
43.
def ChangeList():
L = [] # List L
L1 = [] # List L1
L2 = [] # List L2
for i in range(len(L1)):
L1.append(L1[i] + L[i])
L2.append(len(L) - len(L1))
print(L2)
ChangeList()
Answer- [-1]
44..
def Fun1(mylist):
for i in range(len(mylist)):
if mylist[i] % 2 == 0:
mylist[i] /= 2 # Divide even numbers by 2
else:
mylist[i] *= 2 # Multiply odd numbers by 2
Page 27 of 28