[Tutor] Functions !

dman dsh8290@rit.edu
Wed, 29 Aug 2001 00:44:22 -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"...

They are similar.  I never did enough BASIC programming to really call
it programming, but from other posts on this list I believe that BASIC
subroutines have no local variables or arguments -- everything is
global instead.  This is significantly different from functions in
Python (and C and C++ and Java and Eiffel, etc).  Any variable created
inside a function (using the '=' operator) exists only inside that
function.  No other code can see it.  It also "magically" goes away
when the function returns.  Local variables are essential for
modularity.  The other main difference is arguments, which is what you
are asking about below.

| 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"? 

Basically yes.  The caller tells the function which variable to use.
However, inside the function you don't know the name of the caller's
variable and you don't have access to it anyways if it isn't a global
variable.  The solution adopted by many Algol-ish languages (python
included) is what you see below.  When defining the function you list
the formal parameters of the function.  That is, you put a name of
your own choosing in the list of arguments that the function is
supposed to take.  Then, once inside the function, you know that that
name will be bound to the corresponding concrete paramter that is
specified by the caller.  You use the name you specified in the formal
parameter list to reference the variable.  The interpreter (or
compiler) will make the association for you.

| 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.

This is an issue if you only have global variables, and it is a major
problem in any large project.  There is no name clash issues here
because those names only exist inside the scope of the function.  They
have no effect on other functions.
| 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.

When a function is called, the interpreter binds all the names in the
function's formal parameter list to the objects specified by the
caller, but the name gets bound in the function's local scope.  Thus
in the following :

def func( an_arg ) : # 1
    print an_arg     # 2

def main() :  # 3
    foo = "Hello World.  I'm learning functions!"  # 4
    func( foo ) # 5


Lines 1 and 2 define the function.  Line 1 specifies the name of the
function ('func') and that it will take exactly 1 argument.  It also
says that, inside the function, that argument will have the name
"an_arg".  On line 5 I call the function.  In calling the function I
must specify which object should be used as the argument in this
invocation.  I specified the object whose name in the current scope is
"foo".  The interpreter then creates the name "an_arg" in "func"'s
local scope and binds it to the same object that "foo" referred to.
In this example, "foo" is a local variable inside the "main" function
so it is not directly accessible by func.  It can only be accessed
through its argument.

| ...Are my guess at least close?  And would addeding "exit" at the end be
| helpful in anyway?

You could call exit at the end if you wanted to explicitly terminate
the program's operation.  In this example it makes little difference.
If you were writing a GUI application then you would certainly need to
call exit when the "exit" button or menu item was selected.  The other
main use in calling exit is when an error has occurred and you want to
report it back to whoever ran the program.  This is mostly useful in
shell scripts so that the script knows whether your program succeeded
or not.  In Python the exit function is in the sys module so you would
first need to import it.  Example :

import sys
sys.exit() # no argument, defaults to EXIT_SUCCESS (which is a C
           # constant that is equal to zero)

sys.exit( 1 ) # a single integer argument, the meaning is
              # application-dependent, normally zero=>success and
              # non-zero=>failure.