This forum is now read-only. Please use our new forums at discuss.codecademy.com.
What's the difference between the print and the return functions?
And what does the return function actually mean, please?
Unfortunately, there is a character limit so this will be in many parts. First thing to note is that
I'll start with a basic explanation.
return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.
On a more expansive note,
return is the main way that a function returns a value. All functions will return a value, and if there is no
return statement (or
yield but don't worry about that yet), it will return
None. The value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or just printed for the benefit of the human user.
Consider these two programs:
def function_that_prints(): print "I printed" def function_that_returns(): return "I returned" f1 = function_that_prints() f2 = function_that_returns() print "Now let us see what the values of f1 and f2 are" print f1 print f2
The following is what will be seen in the console:
"I printed" "Lets see what the values of f1 and f2 are" None "I returned"
function_that_prints ran, it automatically printed to the console
"I printed". However, the value stored in
None because that function had no
function_that_returns ran, it did not print anything to the console. However, it did return a value, and that value was stored in
f2. When we printed
f2 at the end of the code, we saw
Now let us try another example:
def print_a_number(num): print num def return_a_number(num): return num def add_three(num): return num + 3 f1 = print_a_number(7) f2 = return_a_number(2) print f1 print f2 f3 = add_three(f2) print f3 f4 = add_three(f1) print f4
In this example, we pass the results of a function to another function. Let us see what the console will say -
7 None 2 5 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Notice that we got an error when we tried to pass
f1 had a value of
None, even though it had printed out the correct number. This is why it is important to
return values and not
I hope that this was a good explanation. If you would like more, let me know and I'll be happy to try to clarify the best I can.
print displays the results or information to console where as return is used to return a value
e.g., u have a function to add two numbers and u wanted to use sum of these two numbers in some other part of code so here we collect the sum which is being returned by sum function.
return returns the value to a variable so you can use the value it just returned later in the program.
in other words.
return is to return a value.
I don't get it
Print is what you see on the console, but the return function is what the computer get. Sometimes, we want to see the value of an integer then we print it. And the value of the integer may be returned by some function
I had the same question so I tried the following...
f1 = printanumber(7)
The output was...
return is to send the info back to the function to be stored until chosen for the function to be
printed. If it isn't chosen to be printed, the
return will save the operation to the function.
print will just print/output any given variable or string onto the screen.
return stores the value and this value can be used by the program whenever required. It will not print the value onto the screen.
So basically :
If you want to show something to the user - use print
If you want to use the value which is computed at the end of a function later in the program - use return
print => console
return => function
print basically does what it says. It prints in the console.
return will not print, rather it can be used to assign value. Return gives us a value, or any data that needs to be assigned to a variable or use print statement to print the returned value
thanks for your help
it is kinda hard tho
i no get it
I r8 this 8/8 m8 i cant aprci8 this thought ok m8? thax nice b8 m8
print 'Welcome to the Pig Latin Translator!'
#Start coding here!
original = raw_input("Enter a word:")
original = ""
if len(original) > 0 and original .isalpha():
Thank you :)