[Tutor] Functions !

Dutch dutch@lilrc.org
Tue, 28 Aug 2001 23:23:52 -0400 (EDT)


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:")
        print "Fahrenheit:",celsius_to_fahrenheit(temp)
    elif choice == "f":
        temp = input("Fahrenheit temperature:")
        print "Celsius:",fahrenheit_to_celsius(temp)
    elif choice != "q":
        print_options()
    choice = raw_input("option:")


-Dutch
______________________________________________________________________
David van Popering
Running slackware & Window Maker

"It has been established that having a really user friendly interface
makes it much too easy for aliens to take over the Enterprise."