What is the formal difference between "print" and "return"? [duplicate]
Lets say I define a simple function which will display an integer passed to it:
the output will be the same but and I know that when a return statement is used in a function the output can be used again. Otherwise the value of a print statement cannot be used. But I know this is not the formal definition, Can anyone provide me with a good definition?
6 Answers 6
Dramatically different things. Imagine if I have this python program:
What do you expect to be the output?
Why? Because print takes its arguments/expressions and dumps them to standard output, so in the functions I made up, print will output the value of x , which is hello .
printAndReturn will return x to the caller of the method, so:
ret will have the same value as x , i.e. «hello»
printAndReturnNothing doesn’t return anything, so:
other actually becomes None because that is the default return from a python function. Python functions always return something, but if no return is declared, the function will return None .
Resources
Going through the python tutorial will introduce you to these concepts: http://docs.python.org/tutorial
This example, as usual, demonstrates some new Python features:
The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.
With print() you will display to standard output the value of param1 , while with return you will send param1 to the caller.
The two statements have a very different meaning, and you should not see the same behaviour. Post your whole program and it’ll be easier to point out the difference to you.
Edit: as pointed by others, if you are in an interactive python shell you see the same effect (the value is printed), but that happens because the shell evaluates expressions and prints their output.
In this case, a function with a return statement is evaluated as the parameter of return itself, so the return value is echoed back. Don’t let the interactive shell fool you! 🙂
Simple example to show the difference:
![]()
I’ll start with a basic explanation. print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. 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, print will not in any way affect a function. It is simply there for the human user’s benefit. It is very useful for understanding how a program works and can be used in debugging to check various values in a program without interrupting the program.
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.
В чем разница между return и print?
В этом году дал себе залог освоить хотя бы в каком то виде программирование.
На примере функций в Python возник такой вот вопрос.
Есть две конструкции:
На сколько я понимаю при print мы просто отдаем значение total в консоль и программа завершается, а при return мы возвращаем значение total и в последующем можем использовать его.
Результат при исполнении один и тот же, поэтому и возник вопрос в чем разница между двумя такими способами? Можете дать пару примеров как еще используется return?
Print vs Return in Python
First thing to note is that, return and print are statements, not functions.
Basic Explanation
Print only shows the human user a string representing what is going on inside the computer. Though the computer cannot make use of that printing that is why return is a function that gives back a value. With this value it is often unseen by the human user but it can be used by the computer in further functions
Expansive Explanation
Print is like a console.log version of JavaScript. It is mainly for the user to see how it works. It is very useful to understand how a program works and where you can start debugging to check various values in a program without interrupting the program.
return is the main way that helps a function return a value. All functions will return a value, and if there is no return statement (or yield ), 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:
The following is what will be seen in the console:
When function_prints ran, it automatically printed to the console "I printed" . However, the value stored in f1 is None because that function had no return statement.
When function_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 "I returned"
Now let us try another example:
In this example, we pass the results of a function to another function. Let us see what the console will say
Notice that we got an error when we tried to pass f1 to add_three because 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 print them (except when we want to know what the value is).
Print vs Return in Python
Printing and returning are fundamentally different concepts in Python.
- Printing means displaying a value in the console. To print a value in Python, you call the print() function. After printing a value, you can no longer use it.
- Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword. You can use the returned value later by storing it in a variable.
Example
Here is an example of a function that prints a value. If you call this function, you can see a greeting in the console:
Here is an example of a function that returns a value. When you call this function with a name input, the function gives you back a value. The returned value in this case is a greeting with the name. You can store the return value into a variable and then do what you want with it.
Notice how running this piece of code does not show anything in the console! This is because the function returns a value. It does not print it. But you can print the stored value by calling the print() function on the greeting variable:
Why Print Is Used So Much?
The reason why printing is used so often in tutorials is that it’s the easiest way to see what’s behind a value. Besides, it’s easy to copy the code and run it on a console and instantly see a result.
Running a piece of code that prints a value that a function returns.
Printing is great for visualizing the values you deal with in your program. Otherwise, you cannot know if your code is doing what it’s supposed to do. If a function returns a value, you need to print the value to truly know what it is before you start using the function.
Make Sure to Understand the Difference!
Please, make sure to read your course materials thoroughly. The difference between print and return is something that isn’t explicitly stated in a beginner course. This is because the difference is so fundamental that the course creators won’t even bother mentioning about it. When you understand these concepts return and print better, you realize how silly a comparison it even is :). Comparing return and print in Python is like comparing having a car vs looking at pictures of a car.
Conclusion
Printing and returning are completely different things in Python. As a beginner, you may be confused by these two because they are used in a very similar way in many examples.
- Printing means showing a value in the console.
- Returning means giving back a value from a function.
When a function returns a value, you can store it to a variable and do whatever with it. But if a function only prints a value, it doesn’t return it. Thus, you cannot use it anywhere.