What does return do?

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

wouldnt it be easier just to have a short lesson focusing on return?

I was just playing around with return command and found that it comes into role when using print command outside the function. For example, I ran the following code :

def square(n):

  squared = n ** 2
  squadd = squared + 10
  print ("%d squared is %d.") % (n, squared)
  return squadd
  
square(10)
print (square(5))

And got the following output :

10 squared is 100.
5 squared is 25.
35

If I don’t use return, the output comes as :

10 squared is 100.
5 squared is 25.
None
2 Likes

So the conclusion is that return does exactly what it says, returning (handing back) a value to the function call

Yes, and then it halts processing of the function. Once return is reached, the function is gone until it is called again, and and its local variables are gone forever.

I didn’t understand the explanation until removed “return” from the function.
If you have a function:

def square(n):
return says what will be the result of this function and it’s up to you decide.

It’s like: def square(n) = return xxx

If you put:
def square(n):
squared = n ** 2
return = 2 + 2
print square(10)

#it will always print 4, because we decided that the “result” of def square(n) will always be 2+2, and it doesn’t use n ** 2.

That’s about right, except that return is a statement, not a variable.

That means that rather than
return = 2 + 2

… you would just write
return 2 + 2

… and Python would take a look at that statement, then (1) evaluate the expression (in this case, 2 + 2) to the right of return, (2) pass that value to the calling statement , in this case print(square(10), and (3) terminate the function

I think there’s something broken about the test. I’m getting an error even though the answer looks exactly as explained in the hint. I keep having to use the give me the answer option to move to the next lesson - and the answer presented is what I had in the first place. Frustrating.

Omit the spaces between square and ()

Like this:

square(10)

That’s a good example. Check out what happens when square does not take any arguments and the function is called followed by the Built-in-function ‘print’.

Can the character for placeholder be everything?
for example in this lesson it was %d
can it be %a, %b, %c,…%z, etc?

no, %d indicates the data type (decimal), maybe you would get away with using random letters, but attempting to do so would be bad practice

1 Like

Love your reply.
Based on this I want to clarify smth for myself.
Please see below exercise that aims to teach us how does “call” work.
But as per my understanding the result “10 squared is 100” isn’t actually produced by a call square(10) (cos it is outside of the function?) but by print function. Am I right?

EXERCISE:

def square(n):
“”“Returns the square of a number.”""
squared = n ** 2
print “%d squared is %d.” % (n, squared)
return squared

*# Call the square function on line 10! Make sure to
*# include the number 10 between the parentheses.

square(10)
END OF EXERCISE

And it prints 10 squared is 100.

Sorry, maybe if I misunderstood but still wanted to ask.
'* please disregard * above, i didn’t know how to fix the formatting of this part into bald text by #, lol

I get why you would think so and in some way you are right.

I would define a ‘call’ as activating/invoking a method.

Ask yourself this: “would the result (10 squared is 100) be displayed/produced if the ‘square’ method is never called?”

The call/activation is happening in the line that contains: ‘square(10)’

The answer (for this code sample) should be: no

So the result is produced by a call to the ‘square’ method.

The square method itself, calls another method named ‘print’ (amongst other things). Which will eventually display/produce the result. (So that’s where you are right) But it is the call to the ‘square’ method that invokes this behavior.

So by adding ‘print’ to a method you started a chain reaction (this being a very short chain) of one method calling another. You could experiment with this a bit by defining another method and calling that method from inside the first method. Or the other way around. Then add another method and call that from the second. Effectively building a chain of methods calling/invoking the next method. Which is a powerful skill.

Just make all methods print something like ‘hello, this is method1’ and name the method accordingly.

Hope it helps!

Thanks much!
I guess i did get it, building a chain of methods actually helped.
I also did understand that the print method just displays the result but has nothing to do with calc or algorithm (if we remove print line the calculation still takes place, we just don’t see the result) - it wasn’t obvious for me for some reason:)

My sis also helped me with some more explanation re function-return-call dependency:
If we assume that the function is a contact card in a phone book we can see that a talk cannot happen just because the contact exists. We have to call this contact. Contact card can contain more than one phone number or can contain none, that’s why we have to specify which number are we calling by a return statement.

It’s a bit simplified cos a contact card doesn’t process any calculations (and the function does) but it still helped me to finalize understanding of the concept in general so i thought it might be useful for someone else.