[Tutor] Functions !

alan.gauld@bt.com alan.gauld@bt.com
Wed, 29 Aug 2001 12:17:14 +0100


> The last programming I did was in basic on my Commodore 64 
> and functions almost seem like what I remember as "subroutines"...

Absolutely correct. Except they return values wheras subroutines 
stored the results in global variables.

> Im following a pretty good online tutorial called "Non-Programmers
> Tutorial For Python
> " (http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html).
> 
> Anyway, Im reading through and typing in the examples.  Below 
> is one such example and it works fine...

Josh's tutor is fine, It might be worth comparing similar topics 
in Jeff Elkners "How to think..." or my "Learn to Program..."
tutors too for clarification when in doubt.. In particular 
mine uses QBASIC examples in addition to Python so these should 
be slightly more familiar looking.

> I dont understand the variables "c_temp" or "f_temp" since 
> they are not used anywhere else.

You are correct, they are like temporary variables that 
exist during the life of the function call.

See
http://www.crosswinds.net/~agauld/tutfunc.htm

For a more detailed explanation.

> I'm guessing here too that many parts of a program with all their
> different variables can call the funtion so no one absolute 

Thats right. but

> careful not to use the same label (variable) anywhere else in 
> my program.

this isn't. You can use the same names because the 'parameter' 
names are only seen insoide the function.
For a description of naming issuers see:

http://www.crosswinds.net/~agauld/tutnames.htm

> the program "knows" what is to be used there.

When you call the function you pass in values(called 'arguments')
which are assigned to the parameters for the duration of the 
call.

Take a look at the two urls above and see if it makes sense.

Alan G.