CBSE Class 12 Computer Science Question 68 of 103

Working with Functions — Question 1

Back to all questions
1
Question

Question 1(a)

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

total = 0;
    def sum(arg1, arg2): 
    total = arg1 + arg2;
    print("Total :", total) 
return total;
sum(10, 20);
print("Total :", total)
Answer
total = 0
def sum(arg1, arg2):  
    total = arg1 + arg2
    print("Total :", total) 
    return total
sum(10, 20)
print("Total :", total)
Output
Total : 30
Total : 0
Explanation
  1. There is an indentation error in second line.
  2. The return statement should be indented inside function and it should not end with semicolon.
  3. Function call should not end with semicolon.