[Tutor] trying to understand the logic of functions

Michael Janssen Janssen at rz.uni-frankfurt.de
Tue Mar 23 04:52:14 EST 2004


On Mon, 22 Mar 2004, python_simpleton wrote:

>  I am sure i am thinking so unlogical compared to ya'll

I don't think its unlogical when one is uncomfortable with basic
concepts at the first time. For me its rather refreshing when
someone on turor at python asks questions that are that basically that
I've long forgotten I had to learn those concepts in my first
time also.

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

Not after any keyword. break, continue, import, del are all keywords
that certainly takes no ":" behind. After conditions that's right,
but not only after conditions:

for i in range(10):

So its rather that the ":" comes behind everything that starts a
next level of indentation. Last explanation doesn't explain the
problem but leave it to another problem "when to start a next level
of indetation". You can learn this by expierence (there arn't
much cases that takes a ":"). Or you can look it up in the Language
Reference (cause it's rather basic):

http://www.python.org/doc/current/ref/ref.html

Section 7 deals with if, while, for ... It's named "Compound
statements" in contrast to Section 6 "Simple statements". Here you
find what you're asking for. But be warned: it's mostly a
description of the Python language in a kind of grammar, that itself
isn't trivial to understand. Further it's just an description not
reason *why* something is like it is (there are no reasons just
design decissions).

To reword it simple you will leran from the Reference Manual, that
if, while, for, try, Function-defintions and Class-definitions are
"Compound Statements" that have a ":" followed by a "suite":

keyword [anything what goes here]:
   suite

"Simple Statements" like "del" or "break" arn't followed by a
suite and doesn't take a ":" . What's a suite? It's a list of
further statements.


Perhaps it helps you, when looking into the Reference but I don't
believe it would help many beginners... To repeat myself: you won't
find their any reasons or logic - just definitions and words like
"statement" or "suite" that have explanatorical power for some guys.
What you will find is completeness and this is sometime helpful.





>
> Functions

>  So far in the tutorial variables like a have been given values
> through the = sign. I understand that. The while, if, count and
> max_count ideas have made sense, (except on question I will ask
> about "if" in another email) I have many notes when i tried to
> work this out here are some of them.
>
> a = 23
> b = -23
>
> def my_abs (num):
>     if num < 0:
>         num = -num
>     return num
>
> if my_abs(a) ==my_abs(b):
>     print "The absolute value of", a,"and",b,"are equal"
> else:
>     print "The absolute values of a and b are different"

> I understand that def starts a function definition, my_abs is the
> function name (good name for absolute value example)

> Q. num is a parameter? I don't understand what one is really but
> is it getting the values of a = 23 and b = -23 as a local
> variable? like this num(a,b)? then go on to figure the rest of the
> code? or does num take the global variables one at a time to
> determine which side of 0 they are on.  Does num gets if values
> because of my_abs(a) and my my_abs(b) and 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?)

yes, my_abs gets called twice. Perhaps you feel like both function
cals get mangled somehow beacuse thay are that near together. They
don't get mangled.

Both function calls are evaluated seperate and before the
if-expression gets evaluated. You can think of it as it done in this
order (AFAIK it's *realy* done in this order - but you don't have
to care for):

if my_abs(a) == my_abs(b): # eval the function calls one after another
if 23 == 23:               # eval (23 == 23) expression
if True:                   # handle this

>  Q. return is "returning" a value (i guess that is what it is sup
> to do) but where does it return a value, what is the value and
> where is it going.

as shown above both 23 gets into the if-expression (and is
forgotten after). [this was probably very sloppy speaken ;-) Check
the Reference Manual when you realy want to dive into this.]

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

when you type in "my_abs(-23)" Python runs my_abs with this one
argument. When you type in "my_abs(-23, 23)" Python will run this
also but it will fail, because my_abs is defined to take only one
parameter.

> ok here is the last exercise i even attempted in the tutorial
>
> def hello():
>     print "Hello"
>
> def area (width, height):
>     return width*height
>
> def print_welcome(name):
>     print "Welcome", name
>
> hello()
> hello()
>
> print_welcome("Fred")
> w = 4
> h = 5
> print "width =",w,"height =",h,"area =" area(w,h)
>
> okay here it goes

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

yes, no argument defined, no argument needed. "hello(somewhat)"
would be even an error (try this an python will give you a helpful
message).

> Q. In the second section of code defines the function area() is
> given two parameters? width and height? and like i said before I
> don't understand what return does unless maybe width and height
> drive down in their hot rod and pick up (w,h) and w has 4 dollars
> and h has 5 dollars they return to the area's definition and are
> times by the code return width*height

area gets two arguments w and h in this order and put the values of
both arguments into its defined variables named width and height in
this order. "return width*height" gets "return 4*5" gets "return 20"
which is given back.

[There is a SyntaxError:
print "width =",w,"height =",h,"area =" area(w,h) # comma missing
print "width =",w,"height =",h,"area =", area(w,h)
]


> Q. in the third definition print_welcome is defined as a function
> with name as its parameter it prints "Welcome", name the value of
> name is got with the code print_welcome ("Fred") is this right?

yes

> Q. And of course the code is read line by line and even though
> that is so the output is in the order that the functions are
> called. right? and only if that function makes out put (in this
> example with the print) but not all functions produce output?

print_welcome makes the print on its own. area returns a value and
this is printed via the print statement. Again its like this:

print area(4, 5) # eval the function call
print 20         # handle this

>From the "viewpoint" of print it's no difference if you do this way:

a = area(4, 5)
print a



> I bet that was as confusing to read for ya'll as it was for me to
> think up. maybe you can understand how i went wrong with some of
> my original notes

Perhaps most of the confusion (hey, it's not the worst to allways
keep a bit of confusion...) will settle down, when you fire up the
interactive python interpreter and test what's happens, when you
feed functions like area with different numbers of arguments and
tests like this.


Michael



More information about the Tutor mailing list