How does `Return` work?

I feel you bro. Im just as confused

Sorry, I have to say it.
Better you not to explain. If you can see from the number of likes on you reply after your long explanations life do not become easier.

In your example, if the input_number is 100 wouldnā€™t the return be 100/8?

2 Likes

hey guys i just started phython last week so am a really new beginner can someone explain simply what is the whole concept about return

use @patrickd314 explanation he explains very weak

Ahā€¦ now I get it. This makes sense now. thanks!

Yeah and it doesnā€™t help that everyone is making it more complicated with their examples. Using maths makes it more confusing.

We sense a fear of Maths. That same fear will propagate over other areas of learning, especially if you foster the belief that you donā€™t need to learn something, or the mistaken belief that it is too hard to learn. Pull up your socks, roll up your sleeves and put your shoulder to the wheel. Attack learning; donā€™t run away from it.

9 Likes

Hi, is this the same as a function call?

I believe this is just referring to the line of instructions where the function is called, ā€˜calling statementā€™ being the statement where the function is called. The function call itself is an expression which is just part of that particular statement.

Outstanding explanation, @patrickd314, thank you!

Iā€™m new to Python. I understand the return as the way we have to move the information from the function to the rest of the code. If we donā€™t have the return all the information and calculations disappear when we come back to the main code.
Is this right?

Correct, but only in terms of any variables that were defined in the function. Any globals would still exist, although that is moot.

The exception would be when we pass in a data structure. Any changes to that wonā€™t have to be returned since the structure is still technically not inside the function. Weā€™ve only passed in a reference to it that our function can then access.

Most functions we write will be performing some action on some data and would be expected to return the outcome, assuming it is a single value or a new object of sorts.

The simplest form of function is the predicate, which always returns a boolean.

def is_even(n):
    return n % 2 == 0
def is_odd(n):
    return n % 2 != 0

Another form we could call a pure function is one that like above has no interaction with the outside, does not change anything and always returns a predictable result.

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

Something to think about when determining if there is a need for a return is, ā€˜what does the caller see?ā€™. Above the caller doesnā€™t see the arguments, the function does. The caller sees the return value without regard of what operation took place.

add(6, 7)    # function sees 6 and 7

# caller sees 13

add(a, b):
    print (a + b)

add(14, 28)    # function sees 14 and 28
# we see 42
# caller sees None
1 Like

This is a great explanation. I have already learnt most of what iā€™d call the basics of Python, but this explanation really helped the concept click for me.

Thanks

By trial and error I see why return age is needed, without it, the results would be ā€œNoneā€. However, I donā€™t understand what is really going on, but when I comment the #return age you get ā€œNoneā€.

def calculate_age(current_year, birth_year):
age = current_year - birth_year
#return age
my_age = calculate_age(2049,1993)
dads_age = calculate_age(2049,1953)

print(ā€œI am " + str(my_age) + " years old and my dad is " + str(dads_age) + " years oldā€)

def calculate_age(current_year, birth_year):
age = current_year - birth_year
return age
my_age = calculate_age(2049,1993)
dads_age = calculate_age(2049,1953)

print(ā€œI am " + str(my_age) + " years old and my dad is " + str(dads_age) + " years oldā€)

I am None years old and my dad is None years old
I am 56 years old and my dad is 96 years old

1 Like

Basically return evaluates whatever value is there on the right side of return statement and is passed to the calling site. If a variable is being assigned the value of the called function, then the return value is stored in the variable.

Hello @vibranium12 and welcome to the Codecademy Forums!

Functions, by default, return None when there is no return value specified.

You are then concatening my_age converted to a string, which is the result of calling calculate_age(2049, 1993). This result is None since there is no return value specified.

1 Like

Thank you, that was very helpful

Bumping an old thread ā€“ Thank you!! These explanations really helped my understanding of return, but I have one question on this lesson. I was a bit confused by:

age = current_year - birth_year
return age

Because following how the examples looked i tried this:

return current_year - birth_year

Without involving the age variable it worked just fine. Is there a reason they didnā€™t do it the way I just did in the lesson? And are there any reasons why I might want to keep the age variable? Other examples Iā€™ve seen so far donā€™t use an extra variable in this way

Nothing firm, no. The simple return expression is expedient and perfectly valid (and the way I generally write code). Giving the expression a variable name only aids the reader so they know what the expression represents if they cannot figure it out without going over the context of the function.

1 Like