jefjin / go-tour

Automatically exported from code.google.com/p/go-tour
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

I do not understand how the 27 >=20 can print out before the 9 given the order in which the pow f... #147

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
https://feedback.corp.google.com/#/Report/888738807

Description:
I do not understand how the 27 >=20 can print out before the 9 given the order 
in which the pow function is invoked.  Is there some multithreading going on 
that I am not aware of?

UI language: en
Detected language: en

Original issue reported on code.google.com by feedback...@gmail.com on 14 Aug 2013 at 7:13

GoogleCodeExporter commented 8 years ago
This confused me, too! I had to look at the code for a while before I figured 
it out what was going on. What's tricky here is that the functions are being 
called as params to a print function. Here's the code:

    fmt.Println(
        pow(3, 2, 10),
        pow(3, 3, 20),
    )

First, pow(3, 2, 10) runs. That runs the print statement in the pow function 
(27 >= 20). It also returns lim.

Next pow(3, 3, 20) runs. That returns v.

Now that both pow functions have returned, the print in the main function runs 
and prints lim and v (9 20).

Hope that helps.

Original comment by kbris...@google.com on 7 May 2014 at 11:02

GoogleCodeExporter commented 8 years ago

Original comment by a...@golang.org on 8 May 2014 at 3:18