[Tutor] Functions !

Andrei Kulakov ak@silmarill.org
Wed, 29 Aug 2001 08:12:05 -0400

On Tue, Aug 28, 2001 at 11:23:52PM -0400, Dutch wrote:
> I have a question about functions.  I hope I can ask this clearly (and get
> a clear answer too!)
> The last programming I did was in basic on my Commoidore 64 and functions
> almost seem like what I remember as "subroutines"...
> 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...
> I dont understand the variables "c_temp" or "f_temp" since they are not
> used anywhere else.
> 1.
> I am -guessing- that the part of the program that calls on the function is
> sending a variable to be processed in the function and it is getting
> dumped in c_temp or f_temp. If thats the case, they why is it that the
> original variable is not used, in this case "temp"? 
> 2.
> I'm guessing here too that many parts of a program with all their
> different variables can call the funtion so no one absolute vaiable can be
> used so this "dummy" (c_temp or f_temp) is used and I must therefore be
> careful not to use the same label (variable) anywhere else in my program.
> Im trying to follow the program (reading it) and understand what is
> happening.  It just wasnt clear where this 2 new lables came from and how
> the program "knows" what is to be used there.
> ...Are my guess at least close?  And would addeding "exit" at the end be
> helpful in anyway?
> #converts temperature to fahrenheit or celsius
> def print_options():
>     print "Options:"
>     print " 'p' print options"
>     print " 'c' convert from celsius"
>     print " 'f' convert from fahrenheit"
>     print " 'q' quit the program"
> def celsius_to_fahrenheit(c_temp):
>     return 9.0/5.0*c_temp+32
> def fahrenheit_to_celsius(f_temp):
>     return (f_temp - 32.0)*5.0/9.0
> choice = "p"
> while choice != "q":
>     if choice == "c":
>         temp = input("Celsius temperature:")

Here, you *could* use c_temp instead, like this:
          c_temp = input("Celsius temperature:")

I guess the tutorial uses a different name so as not to give a wrong idea
that the name has to be the same. Variable values get sort of
"transferred" between a caller and a function itself.

def func(var1, blah, temp_var, dracula):

func(3.14, some_var, another, Variable14)

And inside func(), var1 is 3.14, blah has the value of some_var, temp_var
has the value of another, and finally dracula has the value of Variable14.

I'd say this is a failure of your tutorial, this is a very important
mechanism and it should have been explained in detail. In the example they
give, it's not necessary to have different names for caller and callee,
but they have anyway and don't explain why they're different.

- Andrei


Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org