Jump to content
  • Advertisement
Sign in to follow this  

[python] functions help

This topic is 3826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm learning python from a wiki-book http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Defining_Functions and having trouble completely figuring out functions an example code from the books is:
a_var = 10
b_var = 15
e_var = 25
def a_func(a_var):
    print "in a_func a_var = ", a_var
    b_var = 100 + a_var
    d_var = 2 * a_var
    print "in a_func b_var = ", b_var
    print "in a_func d_var = ", d_var
    print "in a_func e_var = ", e_var
    return b_var + 10
c_var = a_func(b_var)
print "a_var = ", a_var
print "b_var = ", b_var
print "c_var = ", c_var
print "d_var = ", d_var
and the output should be :
in a_func a_var =  15
in a_func b_var =  115
in a_func d_var =  30
in a_func e_var =  25
a_var =  10
b_var =  15
c_var =  125
d_var = 

Traceback (most recent call last):
 File "C:\Python24\def2", line 19, in -toplevel-
    print "d_var = ", d_var</source>

NameError: name 'd_var' is not defined
I don't get why a_var would equal 15 inside the function it's not changed from the global variable from what I see. the tutorial says: Inside of the function a_var is 15 since the function is called with a_func(b_var). Since at that point in time b_var is 15, the call to the function is a_func(15) This ends up setting a_var to 15 when it is inside of a_func. ---- but I don't understand this at all or where this "a_func(b_var)" is in the code Does anyone know any other good tuts about functions (or python in general) or have a brief explanation of functions/ def functions? Thanks-- Also is there a reason why sometimes the IDLE stops working after running a few things, I have to go into task manager and end the processes to be able to re-open it. I get a socket error

Share this post

Link to post
Share on other sites
Functions take parameters. For example the function:

def PrintXY(x,y):
print x
print y

takes two parameters, x and y. You call the function like this:


so, inside the function, x=1 and y=2. So it will print '1' and '2'. Very important: x and y are variables *local* to the function PrintXY. The local x is not the same as a global 'x', if something like that exists. They happen to have the same name, but they're not the same variable.

Now, if you call with different values, PrintXY(42,24), then x=42 and y=24. And so on. Instead of contants, you can call it passing variables as parameters. So if you call it:


so parameter x takes the value of a, and parameter y takes the value of b

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!