Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a very simple code in python. I am thinking that it should return the sum of the two numbers but its giving output as "NONE". But why? Can anybody help me to understand that one.
BTW I know how I can add two numbers, but I am desirous to ask that why that code is not giving me required output

What I have tried:

Python
#Its simple python code to add two numbers
def ask(num1,num2):
    sum = num1 + num2

print(ask(4,5))
Posted
Updated 18-Aug-17 2:54am
v2

Your code performs a summation, but it never returns the value (i.e. it never gives back a value to the caller) - it's stored in the sum variable, but nothing else happens with it.

You need this instead:
Python
def ask(num1, num2):
    return num1 + num2

print(ask(4, 5))
 
Share this answer
 
v2
Comments
Jochen Arndt 18-Aug-17 8:19am    
+5
Now you beat me by exactly 0 seconds (it shows for both "Posted 22 secs ago").
This is Python 101 !

How do you think Python will guess that sum is the value you want to return as the answer of ask ?

May be you should follow a tutorial and learn how to return a value from a UDF. One can guess you will learn a lot of useful things.
 
Share this answer
 
It is all clearly expalined in The Python Tutorial — Python 3.4.7rc1 documentation[^].
 
Share this answer
 
You have to return the result from your function using the return statement:
Python
def ask(num1,num2):
    sum = num1 + num2
    return sum

print(ask(4,5))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900