What does return do?

Agree that the example above outputs 100 twice?

now lets remove return:

def square(n):
  """Returns the square of a number."""
  squared = n ** 2

my_number_squared = square(10)
print my_number_squared  # Prints None
print square(10)  # Also prints None

run the code or see the comments for output. so return does literally what is say, its returns/hands back the values. This is very useful, given squared is a local variable within the function, without return we wouldn’t be able to access it outside the function

6 Likes

True, however, when I try when the example below, taking out the Returndoesnt change anything !
image

because you do nothing with the returned value.

However, its more complicated that that. return is a control flow construct, to pass data around. And not specific for this example, but as programs grow larger, control flow is such important. You need separate functions for maintainability. (you do seriously not want a 10,000 line single file with no function or classes, its a nightmare).

so understanding return is very important, you are going to need it later, a lot.

That is always the problems with these concepts, its not just specific about this exercise, its about the bigger picture. Something which is difficult to explain.

12 Likes

If i use print square(10) at the bottom and does not use return it doesn’t give correct result.

Is it because function square is not getting any value in return ?

yea, then the default (None) is returned. Functions always return a value

Hi,
I’m all new to programming and didn’t understand the explanation/answer at the top either.

But I do think I understand it now, after reading trough the comments.

A function will always return a value. As a programmer you have to define which value will be returned by the function. If you don’t define this, the function will automatically return ‘None’

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2


print more_complicated(10)
  

In the above example the function will return ‘None’ , because it doesn’t know which step of the calculation to return. I didn’t tell him!
Does it have to return squared or does it have to return the calculation after that? The programmer decides, not the function.

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2
  return complication

print more_complicated(10)
  

Adding return complication will make the function return 102. And for whatever reason I, as a programmer, could have decided to return squared and the function would have returned 100.

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2
  print complication 


more_complicated(10)
  

This piece of code will print 102. How is that possible? Because the code doesn’t actaully call the answer of the function. The print statement is inside of the function.

Adding print more_complicated(10) further along in your code (outside of the function), will return ‘None’ because inside of the function, nothing was returned. (and it will also print 102 again, because the print statement is still inside of the function.)

55 Likes

Hello there,

I have a question. Here is my example of fuction definition:

def square(n):
“”“Returns the square of a number.”""

squared = n ** 2
return squared

Question: why do we

print (square(10))

but not

print (squared)

?
square is variable created inside a function square(). Because of this, I cannot understand how to use return without looking for specific examples that fits my code.

squared has a local scope, it only exists within the function.

furthermore, for the function to execute you need to call the function.

Hmm… I will ask a little different. Let’s say my code looks like this.

squared = 1
def square(n):

    squared = n ** 2
    return squared

print (square(10))
print (squared)

As I know squared value would not change after calling function and code would print:

100
1

How could I change the value of squared using function?

you could update the squared variable by doing:

squared = square(10)

this will store the returned value in a variable, not print the returned value

Thanks. It seems I finally got it… :slight_smile:

Thank you for your explanation. They should take your explanation to replace theirs.

I was having such trouble at first, even after reading the posts, then i realized, return just passes whatever you want to pass back to the call. It doesn’t matter what is happening inside the function, it can be calculating world peace but it won’t matter unless you tell it to return that value.

n is just what is being passed to the function when you call it. You don’t even have to do something with it.

In this instance, n = 6 and is passed to the function but i never used it. What is returned has nothing to do with the function itself at all. The point is, this is very flexible, you don’t need to feel trapped into the exercise.

def square(n):
  """not (Returns the square of a number.) """
  squared = 10 ** 2
  squared_plus = squared + 2
  print  squared, squared_plus
  return "hi, i have nothing to do with this function"
  

print square(6)
2 Likes

The simplest.
If you ask the function to return what it did, it will return.
If you do not ask, it will not return.
And honestly will write that all it agree to provide you now is just None. And if you want something else, ask it with return.
That’s all.

So I’m sure you, rwaimi, have long since learned this, but I’m chiming in because I read this whole thread, couldn’t make sense of the answers, and then still had to go on YouTube, find a good illustrative video, and watch it a couple of times very carefully before I understood what return does.

Now I think I found a way, using one of the free course’s own exercises to show, very concisely, what return is for, and I hope it helps someone:

n = “Hello”

def string_function(s):
return s + “world”

def string_function_no_return(s):
s = s + “world”

print string_function(n)
#will print what is generated by the function because the function returns the value. Calling a function that returns what it generates when given a particular argument produces the same result as simply writing the value that that you derive through the use of that function taking that argument.

print string_function_no_return(n)
#will print “None” because, although it performs the desired operation, it doesn’t connect that operation to any output. Unless you use the generated value to do something inside the function (e.g. include your print statement inside the function), the new string that the function generates is trapped inside the function, and the function returns “None”.

This is a slight misconception. The function returns nothing since it has no return statement. What is seen by the caller at the other end is this nothing, which Python translates to None. We see this when we print a call to a function that has no return.

>>> def foo(bar):
    print (bar)

    
>>> print(foo('bar'))
bar
None
>>> foo('bar')
bar
>>> 

Notice when we print the return value, Python substitutes None so print() has an argument. When we do not print the return value, nothing gets sent to the console because there is nothing to send.

Oh. Thanks. That’s a little tricky.

1 Like

Yes, it takes a little wrapping around our head a couple times. We don’t really get to the meat of it until we run the code in the shell.

None is Python’s alias for nothing or undefined, and we see this in the shell when nothing is echoed as a final response from the function.

At the first glance, it is confusing to have "print"and“return” in the def and you don’t see any necessity of the “return”. With it or without it, you will get pass and nothing changes as long as you recall the function at line 10 by writing square(10)! it is because line 4 prints a string instead of a calculation result, which does not allow us to see the real function of return. It would be more understandable if there are more than 1 variable in the exercise and by changing what to return shows better what return does.

Maybe I can help put it in easy terms since I’m also new to programming.

When you use return, you’re telling the function “this is the value that you will return after you put something in it or use the function.” So let’s say you changed “return squared” to “return 1”. Then, if you put 10 inside of the function, it would print “10 squared is 100.” because of line 4: " print “%d squared is %d.” % (n, squared)". BUT the actual value that function would return is 1. So if you wrote after the function “print square(10) + square(100)” and run it, you’ll get the number 2 (since 1 + 1 = 2). So if you wanted to square the numbers and use those values, you have to return the value that you want to use.

1 Like