Confused about "return"


#1



Hi everyone,
I think I'm having a hard time understanding what the return statement consists in. If someone could help me understand it that would be great.


 
def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

this is definitely not the same as this:

 
def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
        return x

But I'm not sure I can explain it, I might be a bit confused about the return statement.

Tell me if I'm wrong:
the return statement ends the execution of a function, right? that means that in the second part of code, as return is inside the for loop, the loop will stop running after i=0 . And thus doesn't have the time to double each element inside the list.

Then my second question:
In the first case of my code, why do we need to put "return x" at the end of the function? If we hadn't, wouldn't we still have each element of the list multiplied by 2?

Not sure I was very clear here, if not, please tell me and I'll explain it again :slight_smile:
Thanks for your help!


#2

yes, so far so good. Return (in your second code sample) will make the loop end in its first iteration.

there is one simply way to find out:

aList = [3,5,7]
def double_list1(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

def double_list2(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
        return x
print double_list1(aList)
print "second function:"
print double_list2(aList)

see what happens


#3

Thanks @stetim94

For the second part, my question was more:
What's the difference between

def double_list1(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

and

def double_list2(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2

But your answer made me realize that I just had to try it, which I did and the result:
when printing both results, the first is what we expect, and the second prints "None".

So if we don't "return" at the end of a function, I guess it's as if we never called the function?


#4

I think you understand return a lot more than I did until even recently. I knew to use it because they told me too, but you get it a lot better than most people at this stage do.
As you figured out, the difference between,

def double_list1(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

and

def double_list2(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2

is that with the second one, literally it's like the function doesn't exist because you cannot print or return x once you're outside of the function.

Note: In some IDE's, it will throw an error if you don't have a return statement in a function.


#5

calling the functions with the same list should clearly show the difference?

oops, you manipulate the list, let me modify the code:

def double_list1(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

def double_list2(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
        return x

aList = [3,5,7]
print double_list1(aList)
print "second function:"
bList  = [3,5,7]
print double_list2(bList)

#6

Thanks @kyleaw, I think I get this now!


#7

yes, there's clearly a difference because the return stops the loop after the first iteration for the double_list2.

thanks @stetim94 :slight_smile:


#8

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