What is the purpose of the return statement? How is it different from printing?

When writing code in Python, it's important to understand the purpose and differences between the return statement and the print statement. Both of these statements are used to output information, but they work in different ways and serve different purposes. In this article, we will explore the return statement and how it differs from printing.

Understanding the Return Statement

The return statement is used to end the execution of a function and send a value back to the caller. When a return statement is encountered, the function stops executing and returns the specified value. This value can be of any data type, such as a number, string, list, or even another function.

Here's a simple example that demonstrates the use of the return statement:

def add_numbers(a, b):
    return a + b

result = add_numbers(5, 10)
print(result)  # Output: 15

In this example, the function add_numbers takes two parameters, a and b, and returns their sum. The return statement is used to send the sum back to the caller, which is assigned to the variable result. Finally, the value of result is printed, resulting in the output 15.

Differences Between Return and Print

1. Output Location

The primary difference between the return statement and the print statement is the location of the output.

When a value is returned from a function using the return statement, it is stored in a variable and can be used later in the program. The returned value is not immediately visible, as it is not automatically displayed on the screen. You need to explicitly print or assign it to another variable to see the output.

On the other hand, the print statement is used to directly display information on the screen. It immediately outputs the specified value or expression to the console or standard output without storing it in a variable.

Here's an example that demonstrates the difference:

def calculate_sum(a, b):
    return a + b

result = calculate_sum(5, 10)
print(result)  # Output: 15

print(calculate_sum(2, 3))  # Output: 5

In this example, the function calculate_sum returns the sum of two numbers. In the first line, the result is assigned to the variable result, which is then printed. In the second line, the return value of calculate_sum(2, 3) is directly printed without storing it in a variable.

2. Usability and Purpose

Return statements are primarily used to pass information back to the caller. They allow you to use the result of a function in another part of your program. For example, you can store the return value in a variable, use it in an expression, or pass it as an argument to another function.

On the other hand, print statements are used for debugging or displaying information during program execution. They are primarily used for temporary output and are not meant to be used for calculations or to pass information between functions.

Using the right statement for the right purpose is important for writing clean and maintainable code. If you want to use the result of a function later in your program or perform further calculations on it, you should use the return statement. If you simply want to display information during program execution or check the output of a specific line of code, you can use the print statement.

3. Multiple Values

The return statement allows you to send back a single value from a function. However, what if you want to return multiple values?

In Python, you can return multiple values from a function by using a tuple, list, or any other data structure that can hold multiple values. This allows you to pass back multiple pieces of information without using global variables.

Here's an example that demonstrates returning multiple values:

def calculate_sum_difference(a, b):
    return a + b, a - b

sum, difference = calculate_sum_difference(5, 3)
print(sum)        # Output: 8
print(difference)  # Output: 2

In this example, the function calculate_sum_difference takes two numbers as parameters and returns their sum and difference as a tuple. The values are then unpacked into the variables sum and difference, which are printed separately.

Conclusion

The return statement and print statement serve different purposes when it comes to outputting information. The return statement is used to send a value back to the caller of a function, while the print statement is used to directly display information on the screen.

Understanding the differences between return and print is crucial for writing clean and efficient code. By using the return statement appropriately, you can pass information between functions and reuse the result of a function later in your program. On the other hand, the print statement is useful for checking the output of specific lines of code, displaying information during program execution, or debugging purposes.