inkedmn
05-24-2002, 10:43 PM
This is an brief introduction to the use of functions in python. Hope you enjoy it :)
functions are defined by a 'def' statement, like this:
def functionname():
they are assigned a name that will be used to call them. basically, a function is a piece of code that can be used repeatedly, but needs only to be typed once :).
so, let's do our first function. we'll call this function "printer" because it's going to do just that: print :). here it is:
def printer(x):
print x
pretty simple, eh? the 'x' in the parenthesis after the name of the function is called an argument. in this case, whatever you pass to the printer function will be printed. here are some examples using the python interactive interpreter:
>>> def printer(x):
... print x
...
>>> printer('hello')
hello
>>> printer('this is an argument')
this is an argument
ok, now let's try one with multiple arguments. we'll do one that gives the product of two numbers...
def multiplier(x, y):
total = x * y
print total
and here's how it would look in the interpreter:
>>> def multiplier(x, y):
... total = x * y
... print total
...
>>> multiplier(3, 6)
18
>>> multiplier(34, 13)
442
now for something a bit crazier...
we're going to learn how to use the "return" statement. "return" allows you to return a value once the function has finished running. that way, you can assign the value of a function to a variable, and it will be equal to whatever is returned. here's an example:
def something(x):
total = x * 25
return total
now, here's how it'd look when it's run
>>> def something(x):
... total = x * 25
... return total
...
>>> blah = something(4)
>>> print blah
100
>>> blah2 = something(18)
>>> print blah2
450
there's just one more point i feel i should make regarding functions. you've seen alot of variables defined within the functions we've created (like "total" in the last example). these are called "local" variables because they're only accessible within the function. if you try to call "total" outside of the function, it will tell you that function is not defined, like this:
>>> def something(x):
... total = 10
... product = total * x
... print product
...
>>> something(5)
50
>>> print total
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'total' is not defined
make sense?
there are also "global" variables, which can be accessed from outside the function. these variables must be declared as global, like this:
def something(x):
global total
total = 10
product = total * x
print product
now, look what happens when we try to access "total" from outside the function:
>>> def something(x):
... global total
... total = 10
... product = total * x
... print product
...
>>> something(4)
40
>>> print total
10
i must warn you though, global variables are (in my experience)
not very popular with the larger python community and should be used only when absolutely necessary...
well, that's about it.
email me at inkedmn@inkedmn.net if you have any comments/questions, or just post them here...
PACE!
functions are defined by a 'def' statement, like this:
def functionname():
they are assigned a name that will be used to call them. basically, a function is a piece of code that can be used repeatedly, but needs only to be typed once :).
so, let's do our first function. we'll call this function "printer" because it's going to do just that: print :). here it is:
def printer(x):
print x
pretty simple, eh? the 'x' in the parenthesis after the name of the function is called an argument. in this case, whatever you pass to the printer function will be printed. here are some examples using the python interactive interpreter:
>>> def printer(x):
... print x
...
>>> printer('hello')
hello
>>> printer('this is an argument')
this is an argument
ok, now let's try one with multiple arguments. we'll do one that gives the product of two numbers...
def multiplier(x, y):
total = x * y
print total
and here's how it would look in the interpreter:
>>> def multiplier(x, y):
... total = x * y
... print total
...
>>> multiplier(3, 6)
18
>>> multiplier(34, 13)
442
now for something a bit crazier...
we're going to learn how to use the "return" statement. "return" allows you to return a value once the function has finished running. that way, you can assign the value of a function to a variable, and it will be equal to whatever is returned. here's an example:
def something(x):
total = x * 25
return total
now, here's how it'd look when it's run
>>> def something(x):
... total = x * 25
... return total
...
>>> blah = something(4)
>>> print blah
100
>>> blah2 = something(18)
>>> print blah2
450
there's just one more point i feel i should make regarding functions. you've seen alot of variables defined within the functions we've created (like "total" in the last example). these are called "local" variables because they're only accessible within the function. if you try to call "total" outside of the function, it will tell you that function is not defined, like this:
>>> def something(x):
... total = 10
... product = total * x
... print product
...
>>> something(5)
50
>>> print total
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'total' is not defined
make sense?
there are also "global" variables, which can be accessed from outside the function. these variables must be declared as global, like this:
def something(x):
global total
total = 10
product = total * x
print product
now, look what happens when we try to access "total" from outside the function:
>>> def something(x):
... global total
... total = 10
... product = total * x
... print product
...
>>> something(4)
40
>>> print total
10
i must warn you though, global variables are (in my experience)
not very popular with the larger python community and should be used only when absolutely necessary...
well, that's about it.
email me at inkedmn@inkedmn.net if you have any comments/questions, or just post them here...
PACE!