MBOSE Class XII Computer Science · Chapter 2
Functions in Python
Opening soonLearn to write your own Python functions, pass values to them, return results, understand variable scope and use modules and recursion.
17 lessons · 56 practice questions · unit "Computational Thinking and Programming" carries 40 marks
What this chapter covers
- Types of functions
- Defining and calling
- Function header
- Parameters and arguments
- Positional, default and keyword arguments
- Return values (fruitful and void)
- Returning multiple values
- Flow of execution
- Scope (local and global) and the global keyword
- Modules (math, random) and packages
- Docstrings
- Recursion
Key terms
- Function
Named block of code that performs a task when called
- Function header
First line: def, name, parameters, colon
- Parameter
Name in the header that receives a value
- Argument
Value passed in a function call
- Default argument
Parameter with a value used when the argument is skipped
- Keyword argument
Argument passed as name = value
- Fruitful function
Function that returns a value
- Scope
Part of the program where a name can be used
- Module
.py file of functions that can be imported
- Docstring
Documentation string as the first statement
- Recursion
A function calling itself
Important questions with answers
10 of the 56 questions on this chapter, solved.
Question 1 · Multiple choice · 1 mark
Which keyword is used to define a function?
- (a) def
- (b) func
- (c) define
- (d) function
Answer: (a) def
def name(parameters):.Question 2 · Multiple choice · 1 mark
For
def f(a, b): return a - b, what isf(b=3, a=2)?- (a) 1
- (b) 5
- (c) an error
- (d) −1
Answer: (d) −1
Keyword arguments are matched by name: 2 − 3.
Question 3 · Multiple choice · 1 mark
For
def f(x): x = x + 1; return x, aftern = 5andf(n), what doesprint(n)show?- (a) 5
- (b) 6
- (c) None
- (d) an error
Answer: (a) 5
The integer n is immutable; the function changes only its local x.
Question 4 · Multiple choice · 1 mark
Which keyword lets a function modify a global variable?
- (a) global
- (b) nonlocal
- (c) extern
- (d) static
Answer: (a) global
global countinside the function.Question 5 · Assertion and reason · 1 mark
Assertion (A):
def f(a=1, b):is an invalid header.Reason (R): Default parameters must follow non-default parameters.
- (a) Both A and R are true, and R is the correct explanation of A.
- (b) Both A and R are true, but R is not the correct explanation of A.
- (c) A is true, but R is false.
- (d) A is false, but R is true.
Answer: (a) Both A and R are true, and R is the correct explanation of A.
It raises a SyntaxError.
Question 6 · Short answer · 2 marks
Distinguish a parameter and an argument.
Answer
A parameter is the name in the function header; an argument is the value passed in the call.
Question 7 · Short answer · 2 marks
What is RecursionError?
Answer
The error raised when a recursive function calls itself too many times, usually because it has no base case.
Question 8 · Short answer · 3 marks
Give three advantages of user-defined functions.
Answer
Reuse of code, modular and readable programs, easier testing and debugging.
Question 9 · Long answer · 5 marks
Explain positional, default and keyword arguments with examples.
Answer
Section 1.3; Section 8.2 (HSSLC 2020, Q9(a)).
Question 10 · Long answer · 5 marks
Explain scope and the global keyword with examples.
Answer
Section 1.6; Section 8.2 (HSSLC 2025, Q6(a) (first choice)).
Study the whole chapter
17 step-by-step lessons, all 56 practice questions with hints, flashcards and mock tests for MBOSE Class XII Computer Science.