[Tutor] trying to understand the logic of functions

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Mar 23 04:37:37 EST 2004



On Mon, 22 Mar 2004, python_simpleton wrote:

> I have been blown away by the complicated questions and answers, the
> deepth of python is staggering.

Hello!


No: if we're answering in a way that sounds complicated, then that means
that we need to do better to make it less complicated.  *grin* The syntax
--- the rules for putting a computer program together --- should not be
complicated in themselves.


> Q. The : symbol is used at the end of the line in more than just a while
> statement. Is it just used after a control structure (while, if, else,
> elif in the most basic of uses is what i understand so for) is it used
> after a condition and keywords? what is the logic behind it?


The ':' colon shows up in statements like:

    for element in sequence:
        ...
or

    while some_statement_is_true:
        ...

or

    if some_statement_is_true:
        ...


The colon is there just to make it easier to see the beginning of the
block.  In fact, you can think of it in the way that English uses:

    1.  Lists like this.

*grin* Same idea.  Technically, Python might have been able to do without
them, since blocks are already indented.  So the colon is not really for
the machine, but more for us humans who are reading the program, since
we're used to that kind of written convention already.



> def my_abs (num):
>     if num < 0:
>         num = -num
>     return num

[some text rearranged]

>  Q. num is a parameter?

Yes, we'd say that 'num' here is a parameter of the my_abs function.
That is, we can feed my_abs different 'num's.

Let's try playing around with it:

###
>>> def my_abs(num):
...     if num < 0:
...         return -num
...     else:
...         return num
...
>>> my_abs(-42)
42
>>> my_abs(15)
15
>>> my_abs
<function my_abs at 0x60470>
###

Notice that in the last example, since we didn't feed in abs the argument
that it expects, it stays inert.  So only when we pass all the required
arguments to a function, it'll fire off.  If we try making it fire off
without the argument, we'll get an error message:

###
>> my_abs()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: my_abs() takes exactly 1 argument (0 given)
###

But that's fine: we expected Python to say something about this, since it
has no idea what 'num' should be until we tell it.


> this means the function is being called(or whatever the correct word it)
> twice since it the function has only one parameter (num) and num can
> only handle on value at a time(a and b are arguments?)

Whenever Python sees something like:

###
>>> my_abs(-42) + my_abs(-43)
85
###

it can't call my_abs(-42) and my_abs(-43) simultaneously.  Computers are
stupider than that.  *grin*

What's going on behind the scenes is that Python is first calculating
my_abs(-42), storing it temporarily, then calculating my_abs(-43), and
storing that temporarily, and then finally adding those temporary
variables together.  When Python sees something like:

    my_abs(-42) + my_abs(-43)

then it does something sorta like this:

###
>>> x1 = my_abs(-42)
>>> x2 = my_abs(-43)
>>> x1 + x2
85
###

But it can get messy making all these temporary variables, which is why
Python lets us say:

    my_abs(-42) + my_abs(-43)

and let us just get back 85, without having to make more variables.



> Q. is a definition kind of like the rules the code follows in the sense
> that it defines what the function name is how many parameters this
> function can have "it could be" (width, height) and later on code gives
> arguments "values for parameters) in the respective order.

Yes, that's one way to think of it.  Functions let us add new commands to
the Python system.  For example, if we're doing some numerical stuff, we
might want to make a squaring program.  We'd like to be able to say:

###
>>> square(12)
###

and be able to get back 144.  And we can do this, by defining a function:

###
>>> def square(x):
...     """Given a number 'x', returns back its squared value."""
...     return x * x
...
>>> square(12)
144
###

So whenever we need to get the square of some value, we can now use the
square()ing function to do this.  We're giving a name to some kind of
process.  If we're saying something like:

###
>>> 3*3 + 4*4
25
###

it's often worth it to make the human intent more clear by using a named
function like square:

###
>>> five_squared = square(3) + square(4)
>>> five_squared
25
###



> def hello():
>     print "Hello"

> Q. I pretty sure but the first section of code defines hello() as a
> function and every time that function is call like this hello()  it
> prints the string "Hello" oh yeah this does not have a parameter because
> the () have nothing in them?

Yeah, functions that don't have anything in the argument list are written
this way.

I haven't answered all of your questions yet, but let's stop for a moment
and just make sure that things are making more sense now.  *grin*  Do you
have questions on this so far?

Good luck!




More information about the Tutor mailing list