[Tutor] jumping from function to function

bob gailer bgailer at gmail.com
Fri Jan 30 18:56:12 CET 2009

David wrote:
> Dear List,
> the following comes from Harrington's "Hands-on Python" (section 
> 1.11.8): http://www.cs.luc.edu/~anh/python/hands-on/
> <code>
> '''Avoiding any error by passing a parameter'''
> def main():
>     x = 3
>     f(x)
> def f(whatever):
>     print whatever
> main()
> </code>
> I am not quite sure what is going on here. Could you please correct my 
> line of thought?
> 1) main() calls the function def main():
> 2) in function def main(): the actual parameter 3 is given to the 
> function call f().
> 3) f() then "jumps" out of function def main(): to call function
> def f(whatever):, whose new value, 3, gets printed.

1) It is customary to refer to a function by it's name e.g. main()
rather than def main().
2) Distinguish where the function is defined from where it is executed.
The definition of f() is at the same "level" as that of main(), but the
call to f() is nested in main(); it does not "jump out".
> Also: does Python go back to the function call main()'s position after 
> step 3)? 

Yes. when f() is called the position in main() is placed on a "stack".
When f() returns, the position of main is retrieved from the stack and
its execution continues.

Bob Gailer<br>
Chapel Hill NC<br>

More information about the Tutor mailing list