Return Statement in Python
Python return statement is used to exit the function and return a value. The return keyword is used to specify a value that a function should return when a function is called. It performs some calculations or operations and then returns the value or a set of values to the calling code.
Syntax of Return Statement
def function_name(parameters): # The function body return value
Example:
In this example, we can see that when the ‘add_number’ function is called, it returns the sum of ‘a’ and ‘b’, which is stored in the result variable. Then using the print statement, the result is printed on the console.
Python3
def add_numbers(a, b): return a + b result = add_numbers( 7 , 9 ) print (result) |
Output:
16
Difference between return and print in Python
In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements.