Yusuf Yiğit Doğan
last year
Yusufcountry asked

What does the return result; line do?

Kelish Rai
Expert
last year
Kelish Rai answered

In this code:

# Function definition
def get_product(number1, number2):
    result = number1 * number2
    return result

# Get integer inputs from the user
n1 = int(input("Enter an integer: "))
n2 = int(input("Enter another integer: "))

# Get the total
total = get_product(n1, n2)

# Print the total
print(total)

This line is where the get_product() function is called:

total = get_product(n1,n2)

Here’s what’s happening: we're calling get_product() and passing it two arguments, n1 and n2. The function then runs, calculates the product, and returns a value. That returned value is what gets stored in the variable total.

Now let’s take another look at the function itself:

def get_product(n1,n2):
    result = n1*n2
    return result

The line return result means the function will send back the value stored in result to wherever the function was called.

Without the return statement, the function would still calculate n1 * n2 and store it in result, but nothing would be passed back to the outside—so the rest of your code wouldn’t have access to the answer.

Note: The return statement is important because it allows you to take the result of a function and use it later in your program. For example, you could use the returned value in further calculations or display it.

Python
This question was asked as part of the Getting started with Python course.