MBOSE Class XI Computer Science · Chapter 7
Functions and Modules
Opening soonHow to write your own functions, pass values in and get values back, where variables live (scope), and how to use ready-made functions from the `math`, `random` and `statistics` modules.
15 lessons · 71 practice questions
What this chapter covers
- Introduction to Python modules: importing a module with
import <module>and with thefromstatement - The
mathmodule (pi,e,sqrt(),ceil(),floor(),pow(),fabs(),sin(),cos(),tan()), therandommodule (random(),randint(),randrange()) and thestatisticsmodule (mean(),median(),mode()) - The NCERT chapter also teaches built-in functions and user-defined functions (parameters, default parameters, return values, flow of execution, scope), and the NCERT exercises use them, so they are taught here as the base for modules
Key terms
- Function
Named group of statements that runs when called
- Parameter
Variable in the function header that receives a value
- Argument
Value given in the function call
- Default parameter
Parameter with a value used when the argument is missing
returnSends a value back to the caller and ends the function
- Scope
Part of the program where a name can be used
- Local / global variable
Defined inside a function / outside all functions
- Module
A Python file with functions and values that can be imported
- Standard library
The modules that come with Python
Important questions with answers
10 of the 71 questions on this chapter, solved.
Question 1 · Multiple choice · 1 mark
The keyword used to define a function is
- (a) function
- (b) def
- (c) define
- (d) fun
Answer: (b) def
Functions are defined with def.
Question 2 · Multiple choice · 1 mark
A variable defined inside a function is
- (a) global
- (b) static
- (c) local
- (d) constant
Answer: (c) local
It is local to that function.
Question 3 · Multiple choice · 1 mark
chr(97) is
- (a) 'a'
- (b) 'b'
- (c) 97
- (d) error
Answer: (a) 'a'
The character with code 97.
Question 4 · Multiple choice · 1 mark
Which cannot be returned by random.randint(1, 6)?
- (a) 1
- (b) 6
- (c) 7
- (d) 3
Answer: (c) 7
The range is 1 to 6, both included.
Question 5 · Assertion and reason · 1 mark
Assertion (A): A function without a return statement returns None.
Reason (R): Python returns None by default.
- (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.
R explains A.
Question 6 · Short answer · 1 mark
Which keyword sends a value back from a function?
Answer
return.Question 7 · Short answer · 2 marks
What is a function? Write two advantages.
Answer
A named block of statements that runs when called. Advantages: reusability and modularity.
Question 8 · Short answer · 3 marks
Write a function to return the sum of the digits of a number.
def digit_sum(n): total = 0 while n > 0: total += n % 10 n //= 10 return total print(digit_sum(2314))Output:
10Answer
The loop (2), the return (1). The sum is 10.
Question 9 · Long answer · 5 marks
Explain user-defined functions: syntax, parameters, arguments and return values, with programs.
Answer
Sections 1.2 and 1.3.
Question 10 · Long answer · 5 marks
Explain the built-in functions
abs,divmod,max,min,pow,sumwith examples.Answer
Section 1.5.
Study the whole chapter
15 step-by-step lessons, all 71 practice questions with hints, flashcards and mock tests for MBOSE Class XI Computer Science.