Anjeev Singh Academy

Anjeev Singh Academy

Chapter 3 – Working with Functions

Class 12 Computer Science With Python Sumita Arora Book Solutions


Check Point 3.1

Question 1: If return statement is not used inside the function, the function will return:

(a) 0 (b) None Object
(c) an arbitrary integer
(d) Error! Functions in Python must have a return statement.

    Answer: (b) None object

    The default return value for a function that does not return any value explicitly (i.e., does not contains return statement) is None.

    Question 2:
    Which of the following keywords marks the beginning of the function block?

    (a) func (b) define (c) def (d) function

      Answer: (c) def

      Reason — The function definition in Python starts with def keyword.

      def function_name(parameters):
      Indented statements

      Question 3:
      What is the area of memory called, which stores the parameters and local variables of a function call ?

      (a) a heap (b) storage area
      (c) a stack (d) an array

      Answer: (c) a stack

      Reason — A stack is the area of memory, which stores the parameters and local variables of a function call.

      Question 4:
      Find the errors in the following function definitions :

      1. def main()
        print (“hello”)
      2. def func2():
        print(2 + 3)
      3. def compute():
        print (x * x)
      4. square (a)
        return a * a

      Answer:

      1. Colon is missing at the end of the function header.
      2. There is no error.
      3. The variable x is not defined.
      4. def keyword at the beginning and colon at the end of function header are missing.

      The corrected function definitions are:

      1. def main():
        print(“hello”)
      2. def func2():
        print(2 + 3)
      3. def compute(x):
        print (x * x)
      4. def square(a):
        return a * a

      Type A: Short Answer Questions / Conceptual Questions (Chapter 3 – Working with Functions)


      Question 1: A program having multiple functions is considered better designed than a program without any functions. Why ?

      Answer: A program having multiple functions is considered better designed than a program without any functions, because –

      (a) It makes program handling easier.
      (b) It avoids ambiguity.
      (c) It reduces the program size,
      (d) It makes a program more readable and understandable to a programmer.

      Question 2:
      What all information does a function header give you about the function ?

      Answer: The first line of the function definition is called Function Header. It begins with the keyword def and ends with a colon (:), specifies the name of the function, parameters and default argument’s value, if any.

      Question 3:
      What do you understand by flow of execution ?

      Answer: The Flow of execution refers to the order in which statements are executed during a program run.

      Question 4: What are arguments ? What are parameters ? How are these two terms different yet related ? Give example.

      Answer: Arguments – In Python, the values are passed to a function through a function call, called Arguments.

      Parameters – In Python, the values are received by a function through the function header/definition called Parameters.

      Arguments are used in the function call statement while parameters are used in the function header.

      Arguments and Parameters are related to each other in the following ways –

      • (i) The values of arguments are passed to the Parameters.
      • (ii) In some cases, parameters and arguments are used interchangeably.
      • (iii) In some cases, the function can change the value of the argument through parameters.

      For example:

      def power(x, y):
              print(x, "^", y, "is", x * y)
      power(5, 3)
      

      In the above example, x and y are parameters while 5, 3 are arguments.

      Question 5:
      What is the utility of : (a) Default Arguments (b) Keyword Arguments ?

      Answer: (a) Default Arguments – Default arguments are used to assign a default value to the parameters, in case parameters do not receive any value through argument, they can use their default value.
      However, we cannot change the order of the arguments in the function call.

      (b) Keyword Arguments – Keyword arguments are useful when you want to specify arguments by their parameter names during a function call.
      This allows us to pass arguments in any order, as long as we specify the parameter names when calling the function.
      It also makes the function call more readable and self-explanatory.

      Question 6:
      Explain with a code example the usage of default arguments and keyword arguments.

      Answer: Default arguments

      def dSum(a, b = 20):
          return a + b
      
      print(dSum(100)) #using the default value
      

      Output:

      120

      In this example, the value for parameter b is not passed function call statement. Function dSum uses the value of b is 20 and returns the sum of 100 and 20.

      Keyword arguments:

      def power(b, p):
          return b**p
      
      print(kSum(b = 4, p = 2)) #using the keyword argument
      

      Output :

      16

      In this example, the arguments are provided in a different order compared to the function header.

      Question 7:
      Describe the different styles of functions in Python using appropriate examples.

      Answer:

      The different styles of functions in Python are as follows :

      Built-in functions — These are pre-defined functions and are always available for use.

      For example:

      name = input("Enter your name: ") 
      name_length = len(name) 
      print("Length of your name:", name_length)
      

      In the above example, print()len()input() etc. are built-in functions.

      User defined functions — These are defined by programmer. For example:

      def calculate_area(length, width): 
               area = length * width 
              return area 
      
      length = 5 
      width = 3 
      result = calculate_area(length, width) 
      print("Area of the rectangle:", result)
      

      In the above example, calculate_area is a user defined function.

      Functions defined in modules — These functions are pre-defined in particular modules and can only be used when corresponding module is imported. For example:

      import math 
      num = 16 
      square_root = math.sqrt(num) 
      print("Square root of", num, ":", square_root)
      

      In the above example, sqrt is a function defined in math module.

      Question – 8: Fruitful function vs non-fruitful function

      Answer:

      Fruitful FunctionNon-fruitful Function
      The functions returning any values are known as Fruitful Functions.

      It is also known as non-void functions.

      Example:

      def sum(a, b):
      return a+b
      The functions that do not return any values are known as Non-fruitful Functions.

      It is also known as void functions.

      Example:

      def sum(a, b):
      print(a+b)
      Question – 9: How a function returns multiple values?

      Answer: In Python, a function can return multiple values as tuples. The return statement must be written as – return value1, value2, value3, …. , value-n

      Question – 10: Scope in Python? Scope resolving rules in python?

      Answer: Scope in any programming language decide the visibility and accessibility of variable or any component of the program. There are two types of scope in Python – Local Scope and Global Scope.

      Python uses the LEGB rules for resolving the scope of a name.

      Question – 11: Local vs Global

      Answer: Variable defined in the main block having Global Scope, while variable defined inside any function having a local scope.

      The global variables are accessible inside the program anywhere, while local variables are accessible inside the function.

      Question – 12: When a global statement is used? Why is its use not recommended?

      Answer: In python, the global statement is used to access and manipulate the global variable inside the function.

      Generally, you are able to access the global variable inside the function, but when you try to assign any new value to that global variable, Python creates a local variable with the same name.

      If you don’t want to create a local variable of the same name, you can use the global statement.

      Why is its use not recommended?

      Using the global statement can lose control over variables and their scope.

      Question – 13: Write the term suitable for the following description?

      (i) A name inside the parentheses of a function header that can receive a value.

      Answer: Parameters

      (ii) An argument passed to a specific parameter using the parameter name.

      Answer: Keyword Argument

      (iii) A value passed to a function parameter.

      Answer: Argument

      (iv) A value assigned to a parameter name in the function header.

      Answer: Default Argument

      (v) A value assigned to a parameter name in the function call.

      Answer: Keyword / Name Argument

      (vi) A name defined outside all function definitions.

      Answer: Global Variable

      (vii) A variable created inside a function body.

      Answer: Local Variable

      Question – 14: Local and Global scope of variables? How can you access a global variable inside the function, if a function has a variable with the same name?

      Answer: Variable defined in the main block having Global Scope, while variable defined inside any function having a local scope.

      The global variables are accessible inside the program anywhere, while local variables are accessible inside the function.

      In Python, a global variable can be accessed from within a function by using the keyword global. However, this is possible only if the function doesn’t already have a locally defined variable with the same name.

      Type B: Application-Based Questions

      Question – 1:
      What are the errors in the following codes? Correct the code and predict output:

      (a) Find errors

      Answer: Error 1: Indentation Error

      Error 2: using of semicolon

      Corrected Code:

      Output:

      Total : 30
      Total : 30

      (b) Find errors

      Answer:

      Output:

      6
      21

      Question 2:
      Find and write the output of the following python code:

      Answer:

      Output:

      300 @ 200
      300 @ 100
      120 @ 100
      300 @ 120

      Question 3:
      Write the flow of execution for the given code:

      def power (b, p) :
            y = b ** p
            return y
      	
      def calcSquare(x) :
            a = power(x, 2)
            return a
      
      n = 5
      result = calcSquare(n) + power(3, 3)
      print(result)
      

      Answer: Line 9 -> Line 10 -> Line 5 -> Line 6 -> Line 1 -> Line 2 -> Line 3 -> Line 6 -> Line 7 -> Line 10 -> Line 11

      Question 4:
      What will the following function return ?

      Answer: Return nothing. There is no return statement.

      Question – 5: What will the following function return?

      Answer: Print nothing, because the return x+y+z statement is executed first. The control will not be reached the print() statement.

      Question – 6: What will be the output of the following programs?

      (i)

      Answer: 1

      1

      1

      (ii)

      Answer: 1

      10

      1

      (iii)

      Answer: 1

      10

      10

      (iv)

      Answer: Hello there!

      Sorry! You cannot copy content of this page. Please contact, in case you want this content.

      Scroll to Top