Introduction to Python Return Value
To make your code reusable, you tend to use functions if the same piece of code is utilized multiple times. The reason being, you can call that function multiple times where so ever required instead of replicating the same piece of code. The function can also return a value to the caller (whenever the function is called), which can even be stored in a variable to be utilized in the main program. In this topic, we are going to learn about Python Return Value. This is how the return value comes into the picture whensoever functions are used in Python.
How does Python Return Value work?
A python function can return a specified value anywhere within that function by using a return statement, which ends the function under execution and then returns a value to the caller.
Note: The return statement within a function does not print the value being returned to the caller.
For Example, the below function converts the temperature from Celsius to Fahrenheit and returns the same to the caller.
# Function to convert the temperature from Celcius to Fahrenheit def temp_converter (Temp_Celsius): return(Temp_Celsius* 9 / 5) + 32 # Main from where the function "temp_converter" is called for temp_Celsius in (20, 25, 30, 40): print(temp_Celsius, ": ", temp_converter (temp_Celsius))
How the above example will iterate?
- forloop is to convert multiple temperature values available in Celcius.
- The printstatement within the loop will be iterated for each value (20, 25, 30, 40), calling the function “temp_converter” over and again by passing a single value at once.
- During iteration #1 of the “for”loop:
- The print statement will printthe value temp_Celsius, that is 20, followed by :
- The print statement will printthe value of temp_Celsius, that is 20, followed by the colon :
- Thereafter the function “temp_converter” is called passing the current value of temp_Celsius that is 20.
- The function “temp_converter” catches the value being passed in the argument “Temp_Celsius” & returnsthe result of (Temp_Celsius* 9 / 5) + 32 which is equivalent to 68.0
- The print statement then prints the returned value after the colon, and the final output for the first iteration looks like 20: 68.0
Thus completing the first iteration of the for a loop.
Similarly, The loop continues for the remaining values, and the final output looks like
Examples of Python Return Value
Let’s discuss some more practical examples on how values are returned in python using the return statement.
In this example, we will learn how to return multiple values using a single return statement in python.
Let’s write a function that returns the square of the argument passed.
- Usually, a function starts with the keyword “def” followed by the function name, which is “square” over here.
- Arguments are followed by the function name within the round braces () separated by a comma if in case multiple arguments are being passed.
- Finally, the closing round brace is followed by a colon.
- Let’s call this function and save the resultant value in “t”.
def square(x,y): return x*x, y*y t = square(2,3) print(t)
print(t) will print the value to the screen, and the result will look like
Let’s check using the “type” attribute on what type of object “t” holds or whats the return type of the square function, returning multiple values.
The output denotes that it’s a tuple.
In this example, we will learn how to catch the returned values in multiple variables.
An alternate way of catching the multiple values being returned by a function is by unwrapping the tuple returned into multiple variables.
This is done by denoting the same number of variables on the assignment operator’s left-hand side, where the function is getting called.
Let’s define the same function.
But this time, instead of catching the values returned into a single variable; we are gonna specify two variables.
xsquared: One for storing the result returned based on the first argument passed to the square function
ysquared: Another for storing the result returned based on the second argument passed to the square function
xsquared, ysquared = square(2,3)
Let’s Print the same:
def square(x,y): return x*x, y*y xsquared, ysquared = square(2,3) print(xsquared) print(ysquared)
Can you determine what’s the type of data or object stored in xsquared & ysquared respectively? (Like we did in Example 1 and figured out that “t” stores a tuple)
Let’s figure it out.
The output denotes that it’s a int.
The output denotes that it’s a int.
It turns out, both of the variables are having the type as int. The reason being, we caught each returned value independently into different variables.
Function with multiple return statements.
Let’s discuss an example with multiple return statements, with conditionals determining which return statement will be executed.
The below function will return the least value to the caller.
def min_val(x,y): if x < y : return x else : return y print( min_val(5,8)) print( min_val(52,8))
We have got two return statements in this function. The first one [ return x ] gets executed if the value of the argument x is less than that of the argument y and ending the scope of the function here itself.
The rest of the code will be skipped, and the control will be passed back to the caller.
Otherwise, the return statement under else: will get executed if the conditional x<y is not true, thus skipping the first return statement [ return x ].
In short, only one return statement will get executed, and the control is passed back to the caller as soon as the return statement is executed within a function.
To make your code replicable and reuseable, you tend to use functions; a function may or may not return a value. A function can also return multiple values; multiple values returned by a function can be caught in multiple ways, depending upon the requirements. A function may have multiple return statements. As soon as the return statement is executed, the control is transferred back to the caller.
This is a guide to Python Return Value. Here we discuss the examples of Python Return Value along with the output and how does it work. You may also have a look at the following articles to learn more –