Re: [Tutor] Best place to learn on the web

Magnus Lycka magnus at
Tue Jun 22 19:05:08 EDT 2004

Kevin wrote:
> I was wondering where the best tutorial on the net would be to learn def
> functions.  I am having trouble learning them.

The best problem is obviously the tutor mailing list! :)

(Apart from the fact that all the Python mailing lists has worked
poorly the last days.)

What's your problem?

We define functions in Python and in other programming languages
for two reasons. We want to reuse code that we use over and over
again, and we want to be able to split up our problem in separate
pieces, so that we don't have to think about all aspects of a problem
at once. We might call this separation of concerns.

An aspect of this separation of concerns is that we want to be 
able to view the interior of a function as a little world of
it's own. We want to be able solve the little problem a function
is involved with in an unrestricted way, without having to worry 
about the rest of the program when we do this. Likewise, we want
to be able to use a function without concern for that functions
interior. When we call a function, we just want to care about
what we put in and what we get pack. It should be like an automat.
If we enter coins, we get jelly-beans in return. To use the jelly-
bean automat we shouldn't have to care about how jelly-beans are
made. And to make the jelly-bean machine, we shouln't have to be
concerned with how the user got his money, or what he does with
the jelly-beans or if he calls them gottisar instead of jelly-

A way to avoid this unwanted coupling between the outside of a function
and it's interior, is something we call name-spaces, or scopes. When
we define variables, names, inside a function, these names only exist
inside the function. The code inside the function can actually see
the variables defined outside the function, and it can manipulate them
as well, it's not entirely sealed off, but it's usually best for the
function to leave exterior things untouched.

When we define a function, we define parameters that the function
requires, and when we want to use the function, we pass these into
the function by placing them after the function name within parenthesis.

We return data to the caller of the function with the return statement.


>>> def add(a, b):
        return a + b

>>> c = add(2,3)
>>> print c

Note that the variables a and b only exist inside the function.

>>> print a

Traceback (most recent call last):
  File "<pyshell#136>", line 1, in -toplevel-
    print a
NameError: name 'a' is not defined
>>> print b

Traceback (most recent call last):
  File "<pyshell#137>", line 1, in -toplevel-
    print b
NameError: name 'b' is not defined

This is a fairly pointless example though. Let's do something more
useful. Let's imagine that we are calculating upper and lower quartiles
and medians. Perhaps 10% and 90% percentiles as well. Without functions
that might look something like this.

There are many more features in function definitions. See for instance

Feel free to ask what you like. Tutorials aren't very interactive...

Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65  mailto:magnus at

More information about the Tutor mailing list