Issue related to: Core Python
Issue Link: https://www.reddit.com/r/learnpython/comments/fvwo1i/asking_for_help_return/
I’m new here, so bare with me. I’ve recently started to learn typing python, but I see “return” alot, but I don’t know what it means. I have an example:
new_age = age * 2 return new_agea = doubled_age(20) print(a)
Some say that “print” and “return” is the same, but I don’t see how? Can you help me?
print() and return are very different
To understand the return statement we need to understand what a function is.
A function is something that takes in an input, transforms the input somehow then gives us the output. The return statement deals with the output section. The best way to imagine a function is with the following worksheet:
Now coming back to Python, We will create a function that will double the users age.
We will call the function “double_age”.
The input we will be taking into the function will be the original age which we will call age.
Thereafter we will fill the function with the process we want. In this care, new_age = age * 2.
Now we need to deal with the output. We usually use a return statement to do this. In this case return new_age which will return the output we want
Now to call the function we will use print(double_age(20)) which will output 40 in the terminal like we want.
def double_aged(age): new_age = age * 2 return new_ageprint(double_age(20) )
Now what about if we did not have a return statement in the function and instead had the following:
def double_aged(age): new_age = age * 2 print(new_age)print(double_age(20) )
In the terminal we have 40 and we have None
You might be asking why do we have None?
This is because print() is a function that has been built by Python so it also follows the same principle;
Takes in an input by you the user and then returns that to the terminal
If print does not have anything to return it will return None
Print() is a function whereas return can be considered a piece of a function