# Function definition on first line.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
return result # Returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7
# This function will return None.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
>>> print(add_two_numbers(5, 7))
None
how it should be (in my opinion :
first command shoud have print() because the method returns a value and does not print it
# Function definition on first line.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
return result # Returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7
# This function will return None.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
>>> print(add_two_numbers(5, 7))
None
The >>> implies this is running in the interactive Python environment/tool. The interactive Python āshellā is a REPL environment ā Read, Evaluate, Print, Loop. The P of REPL is print(). You donāt need an explicit print because the REPL explicitly does that for you.
I can go ahead and do that, but removing print() means that you wonāt see the None being returned in the REPL. Calling the function without print will show the following:
An alternative is to add more verbiage/example to show that the āblankā is indeed None:
#Function definition on first line, explicit return used on final line.
def add_two_numbers(number_one, number_two):
return number_one + number_two
#Calling the function returns the sum of the numbers.
>>> add_two_numbers(3, 4)
7
#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
#Calling the function appears to not return anything at all.
>>> add_two_numbers(5, 7)
>>>
#Using print() with the function call shows that
# the function above is actually returning **None**.
>>> print(add_two_numbers(5, 7))
None
I sorta feel like using print() a lot in examples encourages / implies that print() is the way functions are commonly called when thatās not the case.
But I donāt feel strongly about it, really. Especially not in the first learning exercise.
The thing I do feel very strongly about is making sure students have a clear example of what happens when there isnāt a return in a function.
The errors associated with a function returning None in Python are super-common and really confusing for learners.
Variation one:
#Function definition on first line,
# explicit return used on final line.
def add_two_numbers(number_one, number_two):
return number_one + number_two
#Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7
#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
#Calling the function directly appears to not return anything at all.
>>> add_two_numbers(5, 7)
>>>
#Using print() with the function call shows that
# the function above is actually returning **None**.
>>> print(add_two_numbers(5, 7))
None
Variation Two:
#Function definition on first line, explicit return used on final line.
def add_two_numbers(number_one, number_two):
return number_one + number_two
#Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7
#This function does not have an explicit return.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
#Using print() with the function call returns **None**.
>>> print(add_two_numbers(5, 7))
None
LMK which you prefer, or feel free to edit the PR directly.
I am more or less a beginner and I was more confused about the REPL then the print() function I just learned a lot! It may helps to extend the example like this:
# This function has an explicit return.
def add_two_numbers(number_one, number_two):
return number_one + number_two
# Using print() with the function call returns the sum of the numbers.
>>> print(add_two_numbers(3, 4))
7
# I could also save the return in a variable and print it afterwards.
>>> sum_with_return = add_two_numbers(5, 6)
>>> print(sum_with_return)
7
#This function does not have an explicit return.
# That means, it does not return anything.
def add_two_numbers(number_one, number_two):
result = number_one + number_two
#Using print() with the function call shows that
# the function above is actually returning **None**.
# **None* means, there is really none!
>>> print(add_two_numbers(3, 4))
None
# Same if I save the return in a variable.
# Because there is no return, the variable is empty.
# **None* means, there is really none!
>>> sum_without_return = add_two_numbers(5, 6)
>>> print(sum_without_return)
None