Does a function always need to return something?

Question

In this exercise, the sample function syntax shows a return. Does a function always have to return a value?

Answer

NO, a function does not always have to have an explicit return statement. If the function doesn’t need to provide any results to the calling point, then the return is not needed. However, there will be a value of None which is implicitly returned by Python.

11 Likes

How do we know if the function needs to provide results to the calling point or not?

3 Likes

That is up to you as a programmer. You have to decide. There isn’t a fixed rule for this.

I am afraid you will just have to practice this by building software/programs.

6 Likes

I am not sure I have understood the sentence If the function doesn’t need to provide any results to the calling point, then the return is not needed.
In the previous lesson, “Introduction to Python Functions”, we learned the keyword “return” and the way it is used in exercise No.8, 9 and so on. Howerever, before these exercises, we were taught simple functions with examples of calling them without the use of “return”, and the results were always provided although we did not use “return”. E.g.

def mult_two_add_three(number)
print (number*2 +3)
mult_two_add_three (5) #the result is 13

So, what do you actually mean with the above sentence?

6 Likes

There is a big difference between print and result. The effect of these two keywords is quite different.

Take a look at this code:

def square(x):
    print(x ** 2)

square(3)

Evaluation of this code will result in 9 being printed out to the console. But 9 is not the result of square(3)… Let’s say that we want to implement new function, double_square. But we already have a function that calculates the square so let’s use it:

def square(x):
    print(x ** 2)

def double_square(x):
    print(square(x) * 2)

double_square(3)

This code will result in TypeError: unsupported operand type(s) for *: 'NoneType' and 'int', why? Because the value of square(3) is None. To show this let’s run this code:

def square(x):
    print(x ** 2)

a = square(3)
print(a)

This will result in this output:

9
None

9 is being printed out because of the print statement in the square function, None is the value of a.


To summarize a bit. print is just a function that prints out something to the console, but it does not change value of anything. return is a statement that is used to assign value to the function call. This code:

def square(x):
    return x ** 2

def double_square(x):
    return square(x) * 2

print(double_square(3))

will result in 18 printed out to the console. Because value of square(3) is 9.

32 Likes

Thank you. This clarified a lot of things for me.

3 Likes

You’re very welcome :slight_smile: Glad you found this helpful.

2 Likes

I’ve just began my journey into python so forgive me if my reply isn’t satisfactory. But from what I understand. You use the print() function inside
a self- defined function to immediately print out the code as it’s called but
the return function can be used to pass the result the self defined function into
another self defined function or piece of code further down the line.
def function(“greeting”):
print(“greeting”)
function(“Hello”)

def function2(“greeting”):
return greeting
foo = function2(“Hello”)
print(foo)

print() prints something to console, return hands back data to the caller.

what is more worrying, is that your parameters are strings, why?

i think it should be:

def function(greeting):
   print(greeting)

function("Hello")

now Hello is printed as greeting.

Oh yeah your right I did. Sorry about that. I’ll fix it. Like I said i’m new to
programming. I’ll take my time to proof read what I type the next time so
I don’t throw people off. Thanks for pointing that out to me.