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?
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
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.
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.
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
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
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.
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.