The Formal Distinction Between "print" and "return"
In programming, the functions of "print" and "return" serve distinct purposes. "Print" is used to display information on the standard output stream, while "return" terminates the execution of a function and yields a value.
Consider the following example:
def funct1(param1): print(param1) return param1
In this function, "print(param1)" displays the value of "param1" to the console. The "return(param1)" statement then terminates the function and returns the value of "param1" to the calling function.
"Return" differs from "print" in two crucial ways:
Output Usage:
Function Termination:
To illustrate this further, consider the following code:
def main(): ret = funct1(5) other = funct1(7) print("ret is: %s" % ret) print("other is: %s" % other)
This code calls "funct1" twice, passing different values. The output will be:
5 7 ret is: 5 other is: 7
The value returned by "funct1" in the first call is assigned to the variable "ret" and printed. Similarly, the value returned in the second call is assigned to "other" and printed.
In contrast to "print," "other" can be used to store or manipulate the value returned by "funct1."
The above is the detailed content of What is the fundamental difference between 'print' and 'return' in programming?. For more information, please follow other related articles on the PHP Chinese website!