Core9X
Log inSign up

MBOSE Class XI Computer Science · Chapter 7

Functions and Modules

Opening soon

How 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

See classes open nowLog inMBOSE Class XI opens to new students soon.

What this chapter covers

  • Introduction to Python modules: importing a module with import <module> and with the from statement
  • The math module (pi, e, sqrt(), ceil(), floor(), pow(), fabs(), sin(), cos(), tan()), the random module (random(), randint(), randrange()) and the statistics module (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

return

Sends 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Question 6 · Short answer · 1 mark

    Which keyword sends a value back from a function?

    Answer

    return.

  7. 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.

  8. 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:

    10
    

    Answer

    The loop (2), the return (1). The sum is 10.

  9. 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.

  10. Question 10 · Long answer · 5 marks

    Explain the built-in functions abs, divmod, max, min, pow, sum with 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.

See classes open nowLog inMBOSE Class XI opens to new students soon.
Functions and Modules: MBOSE Class 11 Computer Science notes, important questions and answers · Core9X