Return statement

Often times you will want to write a function that process some data and then returns a value. For example, a function that will make a calculation, store the answer in a variable, and send it back to the line that called the function. This can be done using the return statement inside a function.

We can reuse our previous example:

def usd_to_euro(dollars):
    conversion = dollars * 0.80
    return conversion


usd = int(input('Enter the amount of dollars you would like to convert: '))

euro = usd_to_euro(usd)

print('That is', euro, 'Euro.')

The output:

>>>
Enter the amount of dollars you would like to convert: 50
That is 40.0 Euro.
>>>

We’ve first defined a function called usd_to_euro that takes one parameter. The conversion is then made and the value is returned. The line euro = usd_to_euro(usd) calls a function and passes it the argument of 50. The calculated value is then stored in the variable euro.

Geek University 2022