The term return value refers the value that a function gives back. For example in max(42, 17)
we say that “the function max
returned the value 42
.”
Not only can you pass an argument value into a function, a function can also produce a value. You have already seen this in some previous functions that you have used. For example, len
takes a list or string as an argument value and returns a number, the length of that list or string. range
takes an integer as an argument value and returns a list containing all the numbers from 0 up to that parameter value.
Functions that return values are sometimes called fruitful functions. In many other languages, a function that doesn’t return a value is called a procedure, but we will stick here with the Python way of also calling it a function, or if we want to stress it, a non-fruitful function.
How do we write our own fruitful function? Let’s start by creating a very simple mathematical function that we will call square
. The square function will take one number as a argument and return the result of squaring that number.
The return statement is followed by an expression which is evaluated, and then returned to the caller as the “fruit” of calling this function.
There is one more aspect of function return values that should be noted. All Python functions return the special value None
unless there is an explicit return statement with a value other than None
. Consider the following common mistake made by beginning Python programmers. As you step through this example, pay very close attention to the return value. Then look at what is printed when the function has completed.
The problem with this function is that even though it prints the value of the squared input, that value will not be returned to the place where the call was made. Instead, the value None
will be returned. Since line 6 uses the return value as the right-hand side of an assignment statement, squareResult
will have None
as its value and the result printed in line 7 is incorrect. Typically, functions will return values that can be printed or processed in some other way by the caller.
A return statement, once executed, immediately terminates the execution of a function, even if it is not the last statement in the function. In the following code, when line 3 executes, the value 5 is returned and assigned to the variable ret, then printed. Lines 4 and 5 never execute. Run the following code and try making some modifications of it to make sure you understand why “there” and 10 never print out.
The fact that a return statement immediately ends execution of the code block inside a function is important to understand for writing complex programs, and it can also be very useful. The following example is a situation where you can use this to your advantage – and understanding this will help you understand other people’s code better, and be able to walk through code more confidently.
Consider a situation where you want to write a function called longer_than_five
, to find out, from a class attendance list, whether anyone’s first name is longer than five letters. If there is anyone in the class whose first name is longer than five letters, the function should return True
. Otherwise, it should return False
.
In this case, you’ll be using conditional statements in the code that exists in the function body, the code block indented underneath the function definition statement (just like the code that starts with the line print("here")
in the example above – that’s the body of the function weird
, above).
Try to write the code for the example above yourself! Then test it on different lists and make sure that it works. But read the explanation first, so you can be sure you have a solid grasp on these function mechanics.