[Tutor] trying to understand the logic of functions
Gregor Lingl
glingl at aon.at
Wed Apr 7 03:13:14 EDT 2004
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 [object
(i. e. const or name or expression (*))],
and returns a result [return value, i.e. another object],
or performs an action;
3. variables are the names for objects;
parameters are the names used in the definition of a function,
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 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 names;
5. statements can assign names to objects; an example is a
statement which assigns a name to the object returned by a function
e.g.:
name = function (argument);
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)
>>> table(square)
0 0
1 1
2 4
3 9
4 16
>>> table(cubus)
0 0
1 1
2 8
3 27
4 64
>>>
(*) strictly speaking constants and names are also expression (albeit
simple ones in
contrast to compound expressions)
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