[Tutor] jumping from function to function

Alan Gauld alan.gauld at btinternet.com
Fri Jan 30 19:40:55 CET 2009


"David" <ldl08 at gmx.net> wrote

> def main():
>     x = 3
>     f(x)
>
> def f(whatever):
>     print whatever
>
> main()

> I am not quite sure what is going on here. Could you please correct 
> my line of thought?

As others have said you are pretty much correct apart from some
terminology.

However one of the insanely great things about Python is that it is
very easy to test these kinds of assumptions by writing code at
the >>> prompt:

>>> def main():
...        f(1)
...       g(2)
...        f(3)
...
>>> def f(v): print "This is f with parameter: ", v
...
>>> def g(v): print "This is g with parameter: ", v
...
>>> main()
This is f with parameter:  1
This is g with parameter:  2
This is f with parameter:  3
>>>

Here you can see quite clearly that main() executed the
main function which in turn main called f followed
by g followed by f again passing in different values
each time. You can see the printout showing that the
functions f and g used the values passed in by main
as substitutes for v.

This kind of experimental learning is a very powerful tool
when you come up with questions like this. Don't be afraid
to tamper with the example code and see if it does what
you expect. If it doesn't try to figure out why not. If you
can't then come back here and ask! :-)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list