What is the difference between print and return?


#1

I’ve seen the return statement used in several exercises now and I’m not really sure what the different is between return and print. I know that print posts whatever we put into it in the terminal window, but what does return do?


#2

return does literally what it says, it returns something/hands you something back.

we can then print the returned result, or store it in a variable


#3

I’m still having some trouble understanding why we need it. Couldn’t we just print, say the result we are looking for, or store a result in a variable anyway without it?


#4

lets say we have the following code:

def add(x, y):
   z = x + y

def multiply(a):
   print a * 3

add(3,4)

i want to multiple the result of the add() function, with return we can achieve this, given we can return the result of the add function, and then use it for the multiplication.


#5

So to my understanding, return is used when you have parts in your code that you want the program to take into account before it executes all of the code that we have. So, say I have a function whose result I need for a function that follows it, then that result is used for another function, etc. I would want the program to take into account that these functions depend on each other in a sense. Is that more or less right?


#6

Stetim for the sake of simplicity you might want to go over what print does.

Print purely sends something to the command line to be viewed. When you go through a print you are seeing that value displayed, it does not send a value back it just prints it out in the command line.

When you return a value your function will return that value to whatever you were doing.

Say you has
def add(x, y):
z = x + y
return z

New_Varaiable = add(3,4)

New_Variable will be set to the value of what was returned in our case Z was returned which equals 7 vs if you printed the statement instead of returning it you would just see a value in the command line but not be able to use it because its not stored.

You can call New_Variable in other operations like

Multiply_Variable = New_Variable * 3
=21

I hope this helps clear some questions up but it may cause new ones.


#7

So it’s as if return is holding on to some value to be used for something else. Right?

But also, I have some questions about the code you have there…

  1. how is it that New_Variable is set to what was returned from z? Does the program just know that by using return we are asking it to use that value in the code that comes after it?

  2. Also, if z stands for the addition of 3 and 4, don’t we have to define x and y as such? And if we don’t, how does the program know that x=3 and y = 4? And if that isn’t part of that at all, what is add(3,4) doing exactly?


#8

Yea you are defining a function, that function will return a value to wherever you are calling it from.

add(4,2)

  1. Yep you answered your own questions, returning z is just returning the value of the added items to be used in other areas

  2. Your function is taking on two variables x and y, when you pass values to it your X and Y in the function now take on those values like storing variables in the early chapters.

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

when you run add(4,3)
the function is now passing 4 to x so that X=4 and 3 to y so that Y = 3
Z then becomes the product of those and is returned.


#9

stetim94 and humanoptimization, I feel like I understand what return does, now I need to get some more practice with visualizing it and practicing with it. Thank you for your patience and being very thorough.


#10

@humanoptimization, see here:

https://legacy.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions

please follow variable naming convention for python, so it should be new_variable or cammelCase (newVariable), although the underscore is preferred

@awilga1, we can also print the returned value:

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

def multiply(a):
   print a * 3

print add(3,4)

so the variable name in which we store the returned result can be anything.

the problem is that z has a local variable scope, we can’t access it outside the function:

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

add(3,4)
print z # error

which is where return comes in.

The code i used in these examples could a simple calculator, but you don’t know up front what operations (multiply, add, subtract, division) the user wants to do, and with what values/numbers.

so we need to be able to get values from a function (using return) so we can do all the operations (which might be several) the user wants to do on our calculator.


#11

Once again in words:

Return returns anything from a function (value, text, whatever)

Print prints something to your screen (output value, text, whatever to screen)

So return and print are very different!


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.