KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Predict the output of the following code:

def Add(num1, num2):
    sum = num1 + num2
sum = Add(20, 30)
print(sum)

Python Functions

3 Likes

Answer

Output
None
Explanation

The function Add() is called with the values 20 and 30. Inside the function, the statement sum = num1 + num2 calculates the addition and stores the result in the local variable sum. However, the function does not contain a return statement to send the value back to the main program. Therefore, Python automatically returns None. In the main program, the same variable name sum is used as sum = Add(20, 30), so the value None gets stored in it. Hence, the print(sum) statement displays None.

Answered By

3 Likes


Related Questions