# Purpose of return

#1

Can someone tell me the point of return squared . Without it, it still gives the same result.
Perhaps I do not understand functions properly.
Could someone help?

``````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 9! Make sure to
# include the number 10 between the parentheses.
square(10)``````

#2

A function can do these things in general..

1.Print something when you call it.
2.Return something when you call it and to catch the value you need to use any variable.
3.You can print and also return the value.

In your case 3rd is happening.

``````print "%d squared is %d." % (n, squared)
return squared``````

As you're calling the function ,It will print-> at 10 --> `10 squared is 100.`
But as your function is returning the value but your not using it.
so It does not show what it returned.
To use your return value,you need to do this..

``````x = square(10) # catching the returned value inside a variable
print x``````

It will show this..

``````10 squared is 100.
100``````

#3

a quick example:

``````def multiply(x,y):
return x * y

return x + y

def division(x, y):
return x / y

a = multiply(3, 5)
c = division(a,b)
print c``````

`return` literally means the function hands you something back. So in `a` store the result of the multiplication of 3 and 5 (15), in `b` i store t he result of adding 3 and 5 together (8)

then i divide b by a (16 / 8, which is 2) which i store in c, which i then print (2)

so return literally means the function hands you something back

you can print what a function returns by adding a print statement in front of function or you can store the returned result in a variable, so you could use the result later again (like i did with `a` and `b` in my example)

the exercise might print the returned result without adding print in front of the function call, this is not standard outside of codecademy

#4

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