[Type B] Working with Functions – Chapter 3 Sumita Arora

|

Working with Functions in Python Class 12 is chapter 3 from Sumita Arora book. There are function in python practice questions below, specifically type B or application based questions. We’ve also covered:

  • flow of execution in python
  • error finding questions in python
  • output based questions in python
  • function in python types (type A)
    • user-defined functions in python (type A)
    • built-in functions in python (type A)
  • function in python example (type A)
  • function in python with return type
  • function in python with arguments

These all are topics from function in python class 12 chapter. You’ll also learn types of functions in python (type A) and practice some output based questions for class 12 level with answers. Keep scrolling to learn more! Type B questions are more containing function error finding questions in python class 12.

Table of Contents

Type B: Application Based Questions

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

Solution: Let’s correct the code and identify the errors.

1. (a) Python
total = 0;
   def sum( argi, arg2):
   total = arg1 + arg2;
   print("Total:", total)
return total;
sum( 10, 20);
print("Total:", total)
1. (a) Solution
# Error 1: Inconsistent indentation
total = 0  # Removed semicolon unnecessary in Python
def sum(arg1, arg2):  # Corrected parameter name
    total = arg1 + arg2  # Corrected parameter name and indentation
    print("Total:", total)
    return total  # Indentation corrected
result = sum(10, 20)  # Assigned the return value to a variable
print("Total:", result)  # Accessing the correct total variable
  • (a) In short, Error: total inside the function sum is a local variable and doesn’t affect the global total.

In detail, there were issues with indentation, semicolon usage, incorrect parameter names, and incorrect return variable usage. These were corrected accordingly.

Changes:

  • The error was in using argi instead of arg1 in the function parameters. It’s crucial to use the consistent names to avoid confusion and errors.
  • The keyword for returning a value in Python is return, not RETURN. Python is case-sensitive, so using the correct casing is essential.
Output
Total: 30
Total: 0
1. (b) Python
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])
1. (b) Solution
# Error 1: Missing colon at the end of function definition
# Error 2: Incorrect capitalization of 'range'
# Error 3: Incorrect keyword 'RETURN' instead of 'return'
# Error 4: Incorrect syntax for function calls
def Tot(Number):  # Corrected function definition
    Sum = 0
    for C in range(1, Number + 1):  # Corrected capitalization and syntax
        Sum += C
    return Sum  # Corrected return keyword

print(Tot(3))  # Corrected syntax for function calls
print(Tot(6))  # Corrected syntax for function calls
  • (b) In short, Error: Range should be range, and RETURN should be return.

In detail, errors included missing colons, incorrect capitalization of ‘range’, incorrect usage of ‘RETURN’ instead of ‘return’, and incorrect syntax for function calls. These were fixed.

Changes:

  • The keyword for returning a value in Python is return, not RETURN.
  • The correct function name to generate a range of numbers in Python is range, not Range. Python is case-sensitive, so using the correct casing is essential.
Output
6
21
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *