[Tutor] trying to understand the logic of functions

Sean 'Shaleh' Perry shaleh at speakeasy.net
Tue Mar 23 01:20:15 EST 2004


On Monday 22 March 2004 20:47, python_simpleton wrote:
> I have been blown away by the complicated questions and answers, the deepth
> of python is staggering. And yes I am new at programming and python is my
> first. now to the questions. Oh by the way I have been sticking to
> Non-Programmers Tutorial for Python. I am sure i am thinking so unlogical
> compared to ya'll and i'm taking small "babysteps" but i am trying, sorry
> this is so long but i spent hours reading this tutorial at the airport.
>

no worries.  Programming is a mindset.  For some it comes really easy, for 
others it takes work.  Just remember -- if it ain't fun you need to find a 
new hobby.

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

in most other languages blocks of code are clearly delineated.

Here is an example in C:

int main(int argc, char* argv[]) {
    if (argc == 1) {
        /* in C, there is always one argument, the program's name */
        printf("no arguments given");
    else {
        printf("%d arguments were given", argc);
    }

    exit(0);
}

In Python, blocks are simply marked by indentation so the colon acts as a 
"please start here" indicator to make parsing easier for the interpreter.

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

a function is defined by your code.  At that point it is sitting there idly on 
the sidelines waiting to be called -- just like a player on a sports team.

So the coach (python) says "my_abs, take 'a' and do your thing".  At which 
point my_abs performs whatever operations it was defined to do earlier.

When a function is executed, everything stops and waits for the function to 
finish.  Think of it as a relay sport.  Player 1 grabs the baton ('a') and 
runs his distance.  Then he passes his return value to Player 2.  Player 2 
does his thing and on it goes.  If player 2 fails down, the whole team has to 
wait for him to get up.  Player 3 can't continue the race without the baton.

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

it means 'leave this block and get back to where we started'.  The value of 
the return is an extra parting gift.

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

basically.  You know how to add right?  Take two numbers and you end up with 
one number which is their result.

def add(num1, num2):
    return num1 + num2

A function is the idea, when you call it you make it real.

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

correct, empty parens means no parameters.  A function need not return a 
value, it may just have an internal action.  What if you had code to 
lock/unlock a door:

def lock(enable):
    if enable == True:
        # do something to make the door not open
    else:
        # reverse the above

In programming texts, this is called a side effect.  The function changes 
something each time it is called.

a = 0

def side_effect(num):
    global a
    a += 1
    return num + 1

thing = 6
thing = side_effect(thing)
thing = side_effect(thing)
print thing
print "We changed thing %d times" % a

Notice the function did not actually change thing, we did by assigning the 
result back to thing.  However, it did modify the global variable 'a'.

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

hopefully this makes sense by now .....

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

yep

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

unless you have a control structure you start at the top and read to the 
bottom. Output only happens by print or some other similar structure.

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

nah, many many people have asked these questions before.

Programming is like learning a new language.  You have to learn the grammar 
and the parts of speech and the vocabularly.  Watch out for no-nos (like say 
double negatives in English) and remember that like in spoken language there 
are idioms and slang.

Finally, one of the coolest parts of Python is the fact that you can play in 
the interpreter.

python
>>> def my_abs(num):
...     if num < 0:
...         num = -num
...     return num
... 
>>> my_abs
<function my_abs at 0x4021bcdc>
>>> my_abs(6)
6
>>> my_abs(-6)
6
>>> my_abs(0)
0

Notice my_abs was defined but did nothing until I gave it some input.




More information about the Tutor mailing list