Question:
What is the point of the return function in Python 3?
anonymous
2012-11-15 08:26:12 UTC
I'm trying to learn Python 3, and the lesson that i'm on right now has to deal with modular functions. The return function is one of them. I'm not really understanding what the point of the return function is? Here is one of the examples that it gives:

def square(num):
"""This function calculates the square of a number"""
result = num * num
return result
Three answers:
Russel
2012-11-15 13:35:29 UTC
The best way to understand it IMO is this.



A function is like a box. It's a fundamental programming tool for abstraction. Abstraction is putting a certain job into a little container of it's own so that it's inner working don't get tangled with workings of the rest of your program, and also that you can call it as many times as you like (code reuse). We seperate the inner workings using namespaces and scopes which you'll probably encounter shortly in your course.



Now when we create these boxes with different scopes, we need some way for the rest of the program to communicat with the box and vice verca. We need some way to not only be able to call the function, but to plug some data into to it when we call it and a way for it to return the modified data afterwards. In simply, we need the box to have input and ouput. Input is via the parameters. Output via the return statement.



For the example that you posted, it can be used like this



>>> print(square(2))

4

>>> print(square(5))

25

>>> a = square(5)

>>> print(a)

25

>>>



Now take away the return statement and try it again in the interpreter.



>>> print(square(2))

None

>>> print(square(4))

None

>>> a = square(5)

>>> print(a)

None

>>>



It's a rule that a function has to return something. So if you don't put a return statement then it will return the value None by default. That's an actual value by the way, a null value as it's called.



>>> type(None)



>>>



Lastly it's worth mentioning that this input output abstraction thing is according to the functional programming paradigm. It doesn't always get used like that. Sometimes you have a function which works by "side effect" where it may or may not have input, but does something to some object in the rest of the program and returns None. When we do that we're not using the costruct of a function for it's primary purpose of abstraction, instead we're just using it to group together a bunch of code inside a name that we can call as many times as we want, and the function sometimes takes advantage of certain exceptions about how namespaces are seperated, or it requires some other construct like classes and objects to get that "side effect" job done. We usually call this type of function a "sub routine" or "procedure" and if perfectly valid. But sticking to the functional paradigm can make coding go much more smoothly.



Sometimes when I write a small script I write it out in a function called main() using imaginary functions that don't yet exist. Then I just write out the code for those imaginary functions and it's easy because all you need to write a function is a specification for the input (arguments) it will take and the desired output (return value).



Don't worry if some of this doesn't make sense now. When you encounter certain things you'll remember it and it'll make sense then.
Teoma
2012-11-15 08:46:18 UTC
Look at Return as the end product, you throw eggs & milk in a pan and an omelette is 'returned' ☺

It ends the function and the returned value is usually for use where the function was called from.



print ("The square of 3 is ", square(3));



remove the Return from 'square()' and see the difference. (I don't code Python so don't take my code as a working example ☺)
fantz
2016-10-08 11:32:43 UTC
purposes can both go back a cost or not many times appears like void function(param) or form function(param) form is what you come, in case you dont opt to go back something, you take advantage of void in case you determined that your function needs to go back something, then on the end of the function, you should use go back (and in spite of element you opt to go back yet must be an same go back form as suggested on your header) eg: string funtion() string hi = "hi" go back hi


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...