[Tutor] Paper Rock Scissors game - User's choice not returned properly

Steven D'Aprano steve at pearwood.info
Tue Nov 1 00:30:40 CET 2011

Joel Montes de Oca wrote:

> When the user enters an invalid letter, FUNCTION B calls FUNCTION A. 
> FUNCTION A returns choice to FUNCTION B. FUNCTION B does nothing with 
> the return, FUNCTION MAIN gets nothing to returned to it, thus choice is 
>   |
>   |
>   |__ FUN A
>           |
>           |
>           |_ FUN B
> This is how I understand it. So if I want this to work, I need FUN B to 
> give something back to FUN A so that FUN A will have something to give 
> back to FUN MAIN but that doesn't feel right.

But that's exactly right.

Think of people at a dinner table. Fred turns to Barney and says "Pass 
me the salt please." Barney can't reach the salt, so he turns to Wilma 
and asks the same thing. Wilma hands Barney the salt. Now Barney has to 
actually hand the salt to Fred or else Fred doesn't get anything.

So if you care about getting a value back from something, and passing it 
on elsewhere, you MUST use return.

> Is there a way to say GO TO FUN A instead of calling the function?

No. That's what a function call is: it's a safe goto. (Technically, it's 
more like GOSUB if you remember your BASIC.)


More information about the Tutor mailing list