EXTREME NOOBIE

Alex Martelli alex at magenta.com
Tue Aug 29 07:39:17 EDT 2000


"skeetor" <skeetornospam at bellsouth.net> wrote in message
news:q8Kq5.2021$pJ.32209 at news2.atl...
> Whats this i hear about a function?  I just downloaded  this Python thing,
> and i am going through the online tutorials when I get to the "defining of
a
> function..."  The tutorial is real basic until that part (thats where it
> starts assuming you know how to program, Doh!)
>
> Just wondering  if some kind person can clearly and adequately explain
this
> defining of a function thing...

We can try.

Just like you can "give a name" to a certain value, by "binding" that
name to that value (I'm going to assume you type in stuff at the
interactive Python interpreter, and to show the prompts it will
give you as you do so):

>>> name = "Skeetor!"
>>> print "Hello",name
Hello Skeetor!
>>>

so you can "give a name" to a set of actions and/or computations, by
"defining a function" with that name containing those actions/computations:

>>> def greet(name):
...     print "Hello",name
...
>>>

The keyword 'def' is what tells Python you're going to DEfine a Function;
then, on the same name, follow a space, the name of the function you are
defining, an open-parenthesis, zero or more "arguments" (separated by
commas, if more than one) -- which we'll talk more about in a short
while--, a closed-parenthesis, and a colon.

When you enter this line, the Python interactive interpreter prompt changes
from the usual >>> to ... in order to remind you that you are entering a
function's "body" -- the set of actions and/or computations you want that
function to perform.

Now, you must INDENT -- put some space (4 spaces are customary) at the
start of each line that you want to be part of the function-body.  After
that "indent", you will just type each line normally, as you would do
at the interpreter prompt; note that you can use the "arguments" above
listed "just as if" they were names bound to certain values.  This is
because they ARE names and WILL be "bound to values" when you actually
USE the function you're now defining.

Further note that, when you're done typing that line (indent, then
print, etc), and press Enter, the print statement is NOT immediately
executed (as it would be if you typed it at the normal ">>>" prompt);
rather, the "..." prompt appears again.  The statements you are giving
as the body of your function are being 'saved on one side' by the
Python interpreter for future use; not "executed as you go", but
remembered for _later_ execution, as we'll see.

When you're done with entering the body of your function, just press
Enter, and the normal ">>>" prompt will appear again.

Now, you have a function named "greet", that you can "CALL", with (in
this case) exactly one argument; for example:

>>> greet("Alex")
Hello Alex
>>>

Note that, to call the function, you use its name, then an open
parenthesis, then zero or more values (separated by commas, if
more than one; the number of values you use here must be the same
number as the function expects to receive -- in this case, one).
[Then, at line end, of course an Enter, to terminate the line].

The values you give in the call are bound (for the purposes of
the function's body) to the names of the corresponding arguments;
here, the value "Alex" is bound "inside the function" to the
name 'name'.  Then, the statements in the function's body are
executed.


Now, this tells you how to define and call a function, but it does
not tell you when or why you will want to do so.  Just like playing
chess does not just require learning how you can move the pieces,
but also the purposes you fulfill through those moves, so learning
to program does not just require learning a language's rules (which
in Python's case are not really all that many), but also the various
purposes you fulfill through those rules.

The Python tutorial that comes with the Python distribution 'teaches
the moves', but does not dwell on the purposes -- assuming the reader
can already do some programming, in whatever other language, and so,
for example, has some idea of when and why it is appropriate and
useful to define and use functions.

There are several other tutorials on the net that address the
NON-programmer, and teach him/her something about programming
in general, as well as Python.  Try...:

http://www.honors.montana.edu/~jjc/easytut/easytut/
http://www.idi.ntnu.no/~mlh/python/programming.html
http://members.xoom.com/alan_gauld/tutor/tutindex.htm

just for starters; they're each pretty good first-intro's (the
last one larger and more complete than the others), with pretty
different tone and content, so you may find yourself more at
home with one or the other.


Happy learning!


Alex






More information about the Python-list mailing list