[Tutor] trying to understand the logic of functions

peter hodgson py at gogol.humnet.ucla.edu
Mon Apr 12 00:37:00 EDT 2004



Hi Gregor. Thanks. You have certainly given me a lot to think about. I
believe I have grasped the sense of your terms, and put them, IN CAPS,
under my tentative definitions [>], below, just for comparison.

As for your last example, the one about function calls and function
objects, I am still working on that one [see #comments# below]. 

Thanks again.  Peter

On Wed, 07 Apr 2004 Gregor Lingl <glingl at aon.at> wrote:

Hi Peter!

I tried to rewrite your recent posting, just using
different terms. I hope this sheds a different light
on this topic. What do you think of this?

1. a function is defined thus:

   def NAME(LIST OF PARAMETERS):
       STATEMENTS

2. when a function is called:

>   it takes [gets passed] an argument [value or variable],
    it takes [gets passed] an argument [OBJECT (i. e. constant or name or 
                                                expression (*))], 

                   (*) strictly speaking constants and names are also
                       expressions (albeit simple ones in contrast to
                       compound expressions)

>   and returns a result [return value],
    and returns a result [return value, i.e. another OBJECT],
 
  or performs an action;

>3. variables are the boxes for values;
 3. variables are the NAMES for OBJECTS;

>   parameters are the variables used in the definition of a function,
    parameters are the NAMES used in the definition of a function,
>   inside the parentheses, to refer to the value[s] which will be
    inside the parentheses, to refer to the OBJECTS which will be

   passed as arguments when the function is called;

>4. function names, as well as the names of variables [incl. parameters]
 4. function names, as well as PARAMETER NAMES 

   can be arbitrary; they are not what gets stored in
   memory and manipulated by the function when it is called; they are

>   just labelled boxes;
    just NAMES;

>5. statements can assign values to variables; an example is a
 5. statements can assign NAMES to OBJECTS; an example is a

>  statement which assigns the return value of a function to a variable; 
   statement which assigns a NAME to the OBJECT returned by a function
   e.g.:

>   variable = function (argument);
    NAME = function (argument);

>thus, a parameter [which is a variable] can be a function
 thus, a parameter [which is a NAME] can be the RESULT OF A *FUNCTION CALL*,

i.e. its result is the argument of its parent function: e.g.:

   fun1 (fun2()), or even 
   fun1 (fun2(fun3(argument)))
- ----------------------------------------------------

There is a very important difference between a function call and a
function. if you use a function call as argument, the resulting object
is bound to the parameter (name).

But you can also use function(-objects) as arguments, in order to call them
somewhere in the body of the function to be defined. I think this is a 
*somewhat*
advanced topic - therefore here a very short example to clarify it:

 >>> def square(x):
        return x*x

 >>> def cubus(x):
        return x*x*x

 >>> def table(fun):      
       for x in range(5):
          print x, fun(x)  # fun is the function object being called?#

 >>> table(square)         #square is a function object as argument?#
0 0
1 1
2 4
3 9
4 16
 >>> table(cubus)          #cubus is a function object as argument?#
0 0
1 1
2 8
3 27
4 64
 >>>


Regards,
Gregor


peter hodgson schrieb:

>On Monday 22 March 2004 20:47, python_simpleton wrote that terms like
>'parameter' were confusing;
>
>here's another newbie's attempt to sort out parameter's node of terms:
>
>1. a function is defined thus:
>
>   def NAME(LIST OF PARAMETERS):
>       STATEMENTS
>
>2. when a function is called:
>
>   it takes [gets passed] an argument [value or variable], 
>
>   and returns a result [return value],
>   or performs an action;
>
>3. variables are the boxes for values;
>
>   parameters are the variables used in the definition of a function,
>   inside the parentheses, to refer to the value[s] which will be
>   passed as arguments when the function is called;
>
>4. function names, as well as the names of variables [incl.
>   parameters] can be arbitrary; they are not what gets stored in
>   memory and manipulated by the function when it is called; they are
>   just labelled boxes;
>
>5. statements can assign values to variables; an example is a
>   statement which assigns the return value of a function to a
>   variable; e.g.:
>
>   variable = function (argument);
>
>thus, a parameter [which is a variable] can be a function,
>i.e. its result is the argument of its parent function: e.g.:
>
>   fun1 (fun2()), or even 
>   fun1 (fun2(fun3(argument)))
>
>
>
>
>
>
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>




More information about the Tutor mailing list