#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Help with Functions


    So I am currently taking Intro to Game Programming at my local college. We are using the book "Guide to Programming with Python".

    Anyways, I just finished the chapter detailing functions. Im still semi new to programming in general and this book has been awesome in helping me to make sense of everything so far. And I havent really had any problems with our assignments up until now.

    The project Im having trouble with is the following...

    Write a program that has two functions: first() and second(). Function first() should print the string "In function first()" and then call function second(). Function second() should print the string "In function second()". In the global scope, you should call function first().

    My teacher is barely reachable and doesnt respond in a timely fashion. Nor do I have the extra time to spend in class getting the help. So figured Id ask on here. See if anyone can help me out.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    def indent(a,s):
        print('{}{}'.format(' '*a, s))
    
    def one(n):
        indent(n, 'first begins')
        indent(n, 'first calls two')
        two(n + 4)
        indent(n, 'first finishes')
    
    def two(n):
        indent(n, 'two begins')
        indent(n, 'two wastes time')
        indent(n, 'two finishes')
    
    n = 0
    
    indent(n, 'global scope')
    indent(n, 'call one')
    one(n + 4)
    indent(n, 'end')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    So Im assuming thats just a lay out for me? Because if it isnt, im incredibly confused. But even if it is, I still dont entirely get it.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Run the program, look at the output, determine the source of each line of output.

    It seems you should be able to trace the execution by hand.

    You can simplify the problem by first understanding what the indent function does, then you won't need to think about it every time you see it.


    I'll start this for you.

    When python loads a module it runs all the statements at global scope, from top to bottom.

    The statements at global scope start in the leftmost column.


    python therefore
    defines functions indent, one, and two. The identifiers indent, one, and two are now known to python.

    python sets the variable n to 0 n=0.

    python calls indent which prints the message with some preceding spaces.

    python calls indent again with another message.

    We've come to one(n + 4) . python calls the function one, assigning 0 + 4 (that's 4) to the local variable in one named "n".

    Looking now at the source for one we see that python calls indent a few times to display more messages,
    then calls two.

    Look now at the source for two. two prints some stuff via indent, then returns. "two" returns to "one" at the next statement of one, which is
    indent(n, 'first finishes')
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Got it working, and just in time. Thanks alot. Your time is much appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo