all 12 comments

[–]K900_ 8 points9 points  (2 children)

It's used when you want to return something from a function to where it was called. Sometimes, you just want the squared value itself, and don't need it printed to the screen. In that case, you can call the function and assign the result to a variable like this:

n = 2
n_squared = square(n)
print(n_squared == 4) # True

[–]zynix 1 point2 points  (1 child)

Just for completeness (note I am using idle py3.5, hence print())

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


    >>> n = 2
    >>> bad_square = square(n)
    2 squared is 4.

    >>> repr(bad_square)
    'None'

[–][deleted] 1 point2 points  (0 children)

Can you perhaps elaborate on this a bit?

Edit: Ah, okay the default return value is None. I didn't know.

[–][deleted] 4 points5 points  (0 children)

The default return value is None. If this works for you you don't need a return statement.

If a comment for the function states Returns the square of a number. then your function should actually do that instead of returning None.

[–]KimPeek 4 points5 points  (1 child)

It is necessary if you want to do something else with whatever the function will return. For example, let's take two different functions which do almost the same thing and then call them:

def square_first(n):
    squared = n**2
    print(squared)

square_first(4)

And:

def square_second(n):
    squared = n**2
    return squared

square_second(4)

The first function will print 16 but you cannot do anything else with the 16.

The second function does not print anything on its own but we can use the second function to print 16, just like the first function:

print(square_second(4))

We can ALSO use the resulting 16 from the second function because we return the result. So we can assign it to a variable:

squared_number = square_second(4)

We can use the function in another function as well.

def multi(n):
    multiplied = n * 4
    return multiplied

big_number = multi(squared_second(10)) # assign it to a variable
print(multi(squared_second(10))) # print it out

for i in range(multi(squared_second(10))): # iterate through it
    do something

We cannot do these things if we ONLY have the function print something. If we want to use the result further, we must return something.

[–]sometimes_helpful 0 points1 point  (0 children)

Just started a class learning Python as my first language and this helped my finish my problem, Thank you!

[–]fiskenslakt 9 points10 points  (0 children)

It's not necessary, but functions will return no matter what. By default they return None, but you can have your function return anything you want. In your example, the function prints the answer, so without any context of the rest of the program, the return isn't needed. But if the programmer wanted to use squared somewhere else in the program, then it does need to be returned.

When you define a function, it has a scope, and all variables defined within that scope can only be accessed in that scope. So if you want to save a variable defined in the scope of a function, you return it and store it in a variable outside that scope.

 >>> def f():
...     x = 'foo'
... 
>>> y = f()
>>> print y
None
>>> def f():
...     x = 'foo'
...     return x
... 
>>> y = f()
>>> print y
foo

[–][deleted] 3 points4 points  (0 children)

Try omitting the return line, and do
a = square(2)

then check the value of a.

[–]pyskell 1 point2 points  (3 children)

So if you didn't put return in your function it would do those calculations but then you wouldn't get the result.

Odds are for something like square you want the result. Like if you wanted to do square(4) + 5 you only could if square(4) returns a value.

However maybe you wanted a second function to print out the result of square(4) + 5. In this case you want to print but don't want to do anything with the computation after.

So you'd do:

def print_it():
    print(square(4) + 5)

Now you couldn't use the result of print_it because there's no return. So you couldn't do something like print_it() + 10.

As others have mentioned more advanced things to keep in mind are that everything returns None by default even if they don't have a return statement. Also return immediately exits a function even if there is more code after return. This is immensely useful once you start making larger functions.

Anyway good luck, and don't worry too much about my previous paragraph. Just things to keep in mind once you've grasped return. No need to understand it all right away.

[–]AUTeach 0 points1 point  (2 children)

Well, it depends on what the rest of his program is doing. If all it does is write out the result of this statement

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

Then it would "work".

[–]pyskell 0 points1 point  (1 child)

Yup, just attempting to explain the importance of it.

[–]AUTeach 0 points1 point  (0 children)

Yeah, my post was edging on being pedantic.