-3

It is hard for me to understand the return statement.

Below the boolean test if not test(a) is false (means if test(a) is true), else statement returns b.

But just after it returns a, rewrite valuable to a, isn't it?

def proc4(a, b):
    if not test(a):
        b = 'udacity'
    else:
        return b
    return a
David Cain
  • 16,484
  • 14
  • 65
  • 75
Hiro Gotanda
  • 129
  • 2
  • 8

7 Answers7

5

This is a pretty common problem for people to run into: what does it mean when my function has multiple return points?

Setting aside the debate over whether or not multiple returns should ever happen, the fact is that they do. The important thing to remember is this:

A function is finished as soon as it returns.

In every modern programming language I'm familiar with, as soon as a function hits a return point, it quits processing*. If there's a return value, that gets passed back to wherever the function was originally called. Python is no exception.

Your function has a little extra junk in there to make it harder to read, which isn't helping. Specifically, the assignment to b is totally superfluous, because the assigned value is never used. We can rewrite your function like this for clarity, while still explaining multiple returns:

def proc4(a, b): # line 1
    if test(a):  # line 2
        return b # line 3
    return a     # line 4

Now what happens is this. Say that test(a) evaluates to True on line 2. We enter the if block, and encounter line 3: return b. The function now returns the value of b to wherever it was called from, and execution is finished. Line 4 is never executed.

Alternately, if test(a) evaluated to False, then we don't enter the if block. In this case, we skip over line 3, straight to line 4. Now, we execute line 4, and return the value of a to wherever proc4 was called.

*There are certain flow-control statements, such as finally in many languages, that can cause code in a function to be executed after a return statement is encountered. For simplicity and because it's off-topic, I'm not going to go into that in this answer. Thanks to @Racso for pointing out that I missed this!

Community
  • 1
  • 1
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • _"In every modern programming language I'm familiar with, as soon as a function hits a return point, it quits processing"_ - I'm afraid you're wrong. In several languages, the `Finally` block of a `Try...Catch...Finally` statement is executed even if a return line was hit. Check out http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java – Racso Jul 25 '13 at 15:27
  • @Racso Thanks for pointing out that I missed that! I've edited the answer. – Henry Keiter Jul 25 '13 at 15:33
  • No problem :) As you state, the `finally` thing is off-topic; my real intention was to show you that behavior in case you didn't know it. _PS: It's Racso, not Rasco ;)_ – Racso Jul 25 '13 at 16:48
  • @Racso I stared at that *P.S.* for about a full minute before I noticed my typo 0_o – Henry Keiter Jul 25 '13 at 16:55
3

if test(a) is False, set b to 'udacity' and return a

It may as well be written like this.

def proc4(a, b):
    if not test(a):
        b = 'udacity'
        return a
    else:
        return b
Aesthete
  • 18,622
  • 6
  • 36
  • 45
  • 1
    The `b = 'udacity'` is effectively a noop. – ron rothman Jul 24 '13 at 22:56
  • 1
    Worth noting: the original code is a bit foolish, since the local assignment to `b` is irrelevant, as the value is never used. The whole function could just as well be: `proc4 = lambda a, b: b if test(a) else a` – Henry Keiter Jul 24 '13 at 22:57
3

But just after it returns a

Not if it already returned b. After a single return-statement is encountered, the execution of the function ends and a value is returned.

arshajii
  • 127,459
  • 24
  • 238
  • 287
2

Your function might be more clearly written this way:

def proc4(a, b):
    if test(a):
        return b
    else:
        return a

Does that help you understand the logic?

ron rothman
  • 17,348
  • 7
  • 41
  • 43
1

If test(a) is true, the function returns the original value of parameter b.

If test(a) is false, the function returns the original value of parameter a.

By "original value" I mean the value that the function receives.

Racso
  • 2,310
  • 1
  • 18
  • 23
1

I was confused about return when I was new to programming too. In truth it's pretty simple. Suppose we have a calculator application. Here is our addition function:

def add(x,y):
    return x+y
answer = add(1,1)

The answer would be 2. The function does the operation and returns a value as if it was a variable. The function can be assigned to a variable as a value.

Now to your question:If a is not true, b would equal udacity, but would not be returned and a would be returned. Else b would be returned.

verymessi
  • 117
  • 1
  • 12
1

I think you're missing out two other important things

if not test(a):
    b = 'udacity'
else:
    return b

First of all, if a evaluates to true, then your code sets b to the value 'udacity' but doesn't return. Instead, the execution continues to the next block of code. In your case

return a

The if - else is mutually exclusive, meaning that if one block executes (as in it's condition evaluated to true) then the other will be skipped by default. Also the changes you make inside one block won't be available to the other one (your b assignment will never be printed by your else). When your if statement evaluates to false, your code does

return b

which returns whatever is inside b (same applies to 'return a'). About the return statement, the first lines of this Wikipedia entry are really helpful:

In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called

In your case, is just after the point where proc4() is called

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63