# Computer programming: The Return Statement

**URL:** <https://boards.straightdope.com/t/computer-programming-the-return-statement/485769>\
**Category:** Factual Questions\
**Created:** [February 14, 2009, 1:19am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769 "2009-02-14T01:19:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scruloose](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Scruloose](https://boards.straightdope.com/u/Scruloose)\
**Post date:** [February 14, 2009, 1:19am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/1 "2009-02-14T01:19:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kinthalis](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/kinthalis/32/16084_2.png) [@Kinthalis](https://boards.straightdope.com/u/Kinthalis)\
**Post date:** [February 14, 2009, 1:42am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/2 "2009-02-14T01:42:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tetranz](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tetranz](https://boards.straightdope.com/u/tetranz)\
**Post date:** [February 14, 2009, 1:45am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/3 "2009-02-14T01:45:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Scruloose](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Scruloose](https://boards.straightdope.com/u/Scruloose)\
**Post date:** [February 14, 2009, 2:18am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/4 "2009-02-14T02:18:27Z")

</div>

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:

> [@](#):
>
> 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** — known as its return address.

(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!)

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [February 14, 2009, 2:22am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/5 "2009-02-14T02:22:35Z")

</div>

> [@Scruloose](#):
>
> 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.

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.”

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [February 14, 2009, 2:23am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/6 "2009-02-14T02:23:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)\
**Post date:** [February 14, 2009, 2:31am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/7 "2009-02-14T02:31:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Scruloose](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Scruloose](https://boards.straightdope.com/u/Scruloose)\
**Post date:** [February 14, 2009, 2:39am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/8 "2009-02-14T02:39:12Z")

</div>

> [@friedo](#):
>
> 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.”

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!

---

<div class="post-metadata">

**Author:** ![panamajack](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@panamajack](https://boards.straightdope.com/u/panamajack)\
**Post date:** [February 14, 2009, 2:46am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/9 "2009-02-14T02:46:41Z")

</div>

It runs like this:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [February 14, 2009, 3:43am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/10 "2009-02-14T03:43:40Z")

</div>

```auto

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

```

---

<div class="post-metadata">

**Author:** ![ZenBeam](https://avatars.discourse-cdn.com/v4/letter/z/3ab097/32.png) [@ZenBeam](https://boards.straightdope.com/u/ZenBeam)\
**Post date:** [February 14, 2009, 4:01am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/11 "2009-02-14T04:01:59Z")

</div>

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_.

---

<div class="post-metadata">

**Author:** ![RaftPeople](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@RaftPeople](https://boards.straightdope.com/u/RaftPeople)\
**Post date:** [February 14, 2009, 7:17am UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/12 "2009-02-14T07:17:11Z")

</div>

> [@ZenBeam](#):
>
> 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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Apex\_Rogers](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Apex\_Rogers](https://boards.straightdope.com/u/Apex_Rogers)\
**Post date:** [February 16, 2009, 6:56pm UTC](https://boards.straightdope.com/t/computer-programming-the-return-statement/485769/13 "2009-02-16T18:56:17Z")

</div>

> [@RaftPeople](#):
>
> Technically, with out of order execution, speculative branches, etc. it looks like this:
> 
> ```auto
> 
> <--- ^
> \ /
> \ ---->>
> \
> v
> 
> ```

Nicely played.
