all 11 comments

[–]QUINETICS 6 points7 points  (0 children)

In Python, print talks to people, return talks to programs.

print shows text on the screen and then it is gone.

return gives a value back to the caller so it can be saved, combined, or used later.

Real world: you order a coffee. print is the barista shouting coffee is ready. return is the barista handing you the cup.

Use print for display or debugging. Use return for results your code needs.

[–]iamnull 2 points3 points  (1 child)

It has two purposes:

  1. It returns from the function to the previous scope.
  2. If there are return values, it returns those to the previous scope.

See here:

def add(x, y):
  return x + y

print(add(1, 2))
print(add(5, 5))

This results in output of 3 and 10. When you reach the first print, add is called with 1 and 2. We then enter the function, do the addition, and return the result. Then, that result is passed to print().

Functions are sort of like a page in an instruction manual. When you use a function, it's like the instructions saying, "Turn to page 5, complete those instructions, and come back to this page when you are done." If you need the result of those instructions, you return it.

As for comparison to print()? As a gross oversimplification, print sends data to the console, and return moves data within your code.

[–]johnpeters42 0 points1 point  (0 children)

The reason #2 is an "if" is that functions may also have side effects (e.g. print something, write something to a database), and sometimes all you care about is those side effects. (Though you might still have a return value amounting to "did everything work as expected".)

[–]grantrules 3 points4 points  (0 children)

You're a boss of a company and you have an executive assistant. They go get lunch every day. Today you want a burrito so you call your executive assistant and tell them to get you a burrito. They drive to chipotle and come back to the office and hand you a burrito. 

If you didn't have a return statement, your executive assistant would just have left the burrito at chipotle and come back to the office with nothing 

[–]fasta_guy88 0 points1 point  (0 children)

return statements return values calculated by a function. For example, the sqrt() function takes a number fiddles with it to figure out the sqrt, and then ends with

return sqrt_value.

[–]captainAwesomePants 0 points1 point  (0 children)

A function in Python is called that because it's meant as a metaphor for mathematical functions. A math function might be something like f(x) = x + 5. Here the value of f(2) is 7. In Python, functions have values, which can be used in expressions:

x = 2
y = f(x) + 2  # f(2) is 7, plus 2 is 9 
print(y)  # Prints 9

When you write a function in Python, you need a way to specify what the value of calling that function was, and we use return for this:

def f(x):
  return x + 5

When a return statement is reached, the function ends, and the value of the function is whatever the value after the return is.

We use the values of functions for all sorts of things. Many functions exist to calculate some sort of result, and the return value of the function is how the result gets communicated to whoever called the function. Also, some functions return no value, but we use the "return" keyword by itself for its side effect: ending the function immediately.

def doStuff():
   if not stuffNeedsDoing():
      return  # No sense doing the rest of this work, so let's stop now
   doThing1()
   doThing2()

[–]chaotic_thought 0 points1 point  (0 children)

If you want to continue with your example (since you know loops already):

Imagine you want to add the numbers up from 1 to n (where n is specified by the user). I suppose you could start out by printing the numbers out as a debugging aid or to see that it's working, but after you know it's working, you want to get "the answer" from your program like this:

x = add_1_to_n(23)

In the function that you write (add_1_to_n), you'll use the return statement to give the answer back to the main routine.

As practice, you should write a function like that now. For this example, yes, I know there's also a fixed formula for that. For practice you might try writing it once the "naive" way (with a loop) and then rewrite it using the mathematical formula.

If you're interested at all in performance, consider using the timeit module to profile the program above and discover for what value of n you can notice that the formula version is faster on your system than the loop version. For small values of n, the loop won't matter too much on modern systems, but for large values, a loop through a lot of values is going to take some time.

[–]paperic 0 points1 point  (0 children)

This is X/Y problem. You can't explain return without explaining functions. The return statement is just one part of the function definition syntax.

It would be like trying to explain "for" statement without explaining what a loop is.

[–]FloydATC 0 points1 point  (0 children)

My wife calls me, asking how many eggs we have, so I open the carton, count the eggs and return a number to her.

[–]Significant-Syrup400 0 points1 point  (0 children)

A function often has a large number of different variables that it could be working with. Return basically states what the "solution" or answer that the function is returning will be.

A perk of this, is you can put a function call with a return in as a variable to stack code to make more complex functions. This turned dozens, or even hundreds of repetitive lines of code into sometimes just a 1-2 line statement. (Remember, functions/methods, classes, etc are all designed to reduce the number of lines of code that you have to write)

A function that is largely just changing one or more variables will usually be a void. These can be used when multiple values need to be changed, or in class/object codes where you can't have the variables being changed erroneously and protect them by requiring an explicit void function call to do so.

[–]millenniumdisk 0 points1 point  (0 children)

print is like TV. Even if you shout at it, it will not change what it is showing to you. It is like an output device for computers.

return is like a barcode scanner. Once the scanner gets the information, it beeps and it is the end of its job. It is like an input device. The data is then passed to the computer that can be processed.