[Tutor] functions

alan.gauld@bt.com alan.gauld@bt.com
Tue, 29 Jan 2002 10:53:07 -0000


> I dont know why I am having such trouble with this 
> and I have a little perl and php experience, 

Ok, you might try my tutor, reading the topic on 
modules and functions(url below).

> functions. Basically my trouble arises in understanding how things get
> passed into and what the return statement returns.

def function(param1, param2):

Says create a function called 'function' that takes 2
"parameters" called, in this case, "param1" and "param2"

You then write a mini program that treats param1 & 2 as 
if they were local variables.

At some point you can return a result to the outside world
using the "return" keyword.

Thus:

# define a function to add two numbers
def add( x, y):
   return x+y

# now call that function, store result in res
res = add(3,4)  # x,y take on values 3,4
print res       # should print 7

We can also pass variables in:

a = 5
b = 7
print add(a,b)  # prints return value of add(5,7) => 12

Now with that basis what specifically is confusing you?

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld