Computer programming: The Return Statement

I’m currently taking an Introduction to Programming class online. My classmates and I are having a debate on something and the professor hasn’t been around to clarify. Curiosity is getting the best of me, so I’m asking here.

(This doesn’t run afoul of the ‘No homework’ rule because my assignment is already complete and submitted. And as this is my first experience with computer programming, please - use small words and assume I’m an idiot. Because I am.)

Here’s the question:

In the following, to what line does the return statement go?

start
a = 2
b = 4
c = 10
while c > 6
perform changeBAndC()
endwhile
if a = 2 then
perform changeAAndB()
endif
if c = 10 then
perform changeAAndB()
else
perform changeBAndC()
endif
print a, b, c
stop

changeBAndC()
b = b + 1
C = C - 1
return

changeAAndB()
a = a + 1
b = B - 1
return


Let’s just look at the first part:

while c > 6
perform changeBAndC()
endwhile

changeBAndC()
b = b + 1
C = C - 1
return

I’m confused on this point:

A. Does the return statement return control back to the while line after performing the change once, where it re-evaluates if c > 6? This would mean method changeBAndC() runs four separate times, each time returning to the while line.

Or,

B. Does it run once, looping four times, and returning to the endwhile line?

In other words, is the return statement at the end of changeBAndC() executed once or four separate times.

Or,

C. Does it do something else I haven’t thought of?

Our book shows an example of the return statement, and it points to a place in the main program after the method. This makes me think the answer is B. But if that’s the case, how does the method know to run until c <= 6?

Am I making any sense?

The answer is A.

The return statement will dump the execution right after the function call each time. So the return statement is executed 4 times.

The answer is A.

I don’t quite recognize the language but whenever you have something like this:

while SomeCondition

DoSomething

endwhile

Then DoSomething is called repeatedly until SomeCondition is false. The DoSomething doesn’t know anything about the while loop. It just blindly executes and returns to the line after where it was called.

Hmmm… OK, so the answer is A, which is actually what I had assumed when I had to figured the final values of this problem last week. But now that we’re looking at the definition of the return statement, I’m having trouble. This definition from Wiki jives with what our book says:

(emphasis mine)

If I look at the method, I’m not seeing why it returns to the while line to re-evaluate if c > 6, which is before where it was called. Based on the book and the definition above, it should return to the endwhile line, after where it was called.

To me, you seem to be saying the program will run like this:

while c > 6 (c=10)
b = b + 1
C = C - 1
return
while c > 6 (c=9)
b = b + 1
C = C - 1
return
while c > 6 (c=8)
b = b + 1
C = C - 1
return
while c > 6 (c=7)
b = b + 1
C = C - 1
return
while c > 6 (c=6)
b = b + 1
C = C - 1
return
endwhile

What am I missing? Why am I not grasping this?

(I’ll tell you why - not enough friggin pictures and diagrams in the book!)

Your confusion is due to not understanding what “endwhile” does. It doesn’t terminate the loop, it says “here is where the stuff in the loop ends. Everything after me is outside the loop.”

So your return statement does return to the endwhile. Then, the instruction is, “ah ha, this is the end of this while loop, better go and check the condition again, and if it’s true start another iteration of the loop.”

It does return to right before the endwhile. The program then tests the while condition and, if true, jumps back up to the top of the loop.

Yeah, the way a while loop works is it does everything between “while” and “endwhile” over and over until the condition is no longer true.

It returns to right after the function call, and runs “endwhile” which is just a command to go back to the start of the loop. You seem to be thinking it skips over endwhile and continues with the code outside the loop, which is not correct. Or else you’re misunderstanding what “endwhile” means, as friedo suggested.

Ahhh… that’s it. You nailed it. I was confused on how the loop works, not the return statement or method. Now I’m gettin’ it!

Thanks all!

It runs like this:



while c > 6 (c=10)  *TRUE*
perform changeBAndC()
    b = b + 1
    C = C - 1
    return  
endwhile *(this statement just means go back to the while, as explained)*
while c > 6 (c=9) *TRUE*
perform changeBAndC()
    b = b + 1
    C = C - 1
    return
endwhile
while c > 6 (c=8) *TRUE*
perform changeBAndC()
    b = b + 1
    C = C - 1
    return  
endwhile
while c > 6 (c=7) *TRUE*
perform changeBAndC()
    b = b + 1
    C = C - 1
    return  
endwhile
while c > 6 (c=6) *FALSE*
if a=2 then ...

It’s important to note that the function call (the perform changeBandC) is itself a statement of the program. Also note that the while statement checks first, which means the case c=6 does not result in a call to changeBandC.



start
    |
    |
    |
    v
while
^   |
|   |
|   |
|   |
|   |--------------->doSomething()
|                       |
|                       |
|                       |
|                       |
|                       v
|   |<---------------return
|   |
|   |
|   |
|   v
endwhile
    |
    |
    |
    v
stop

The arrow coming down from the endwhile isn’t really correct, because the check is in the while statement. More accurately, a second arrow coming from the while points to just after the endwhile.

Technically, with out of order execution, speculative branches, etc. it looks like this:



     <---     ^
     \       /
      \    ---->>
       \
       v



Nicely played.