[Tutor] functions in Python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Apr 17 19:31:04 CEST 2006


> Sorry, but you have confused me more ;) Can you give an simple example 
> of just function() ? Where can it be useful?
>
> And when you say it returns a value, what do you mean by that? return to 
> whom and what exactly?


One view that's common is the idea that a function is a box that takes an 
input and returns an output:

                 +----------+
     input ----> | function | -----> output
                 +----------+

The most direct equivalent you've experienced to this is a math function.

For example, if we're traveling in a car at fifty miles an hour, for three 
hours, we know that we've traveled 150 miles.  (Word problems... aagggh!) 
But let's write that as a function:

######
def miles_traveled_at_50_mph_for_3_hours():
     return 50 * 3
######

A function definition does not do anything by itself, but it can be 
"called" after it's defined.  Try writing:

#############################################
def miles_traveled_at_50_mph_for_3_hours():
     return 50 * 3

print miles_traveled_at_50_mph_for_3_hours()
#############################################

and run it, and see what you see.  We are "defining" the function at the 
top, and "calling" the function at the bottom,

(Forgive me for such a long function name: please bear with it for a 
moment.)


One natural question we might have here is: how far do we travel if we go 
at the same speed, but for 4 and a half miles instead?  We could just 
calculate it and write it out again:

######
def miles_traveled_at_50_mph_for_4_half_hours():
     return 50 * 4.5
######

But we can do a little better, by allowing our function to take "hours" as 
an parameter:

######
def miles_traveled_at_50_mph(hours):
     return 50 * hours
######

Now we can find out how long we've travelled by computing this function 
for the particular hours we'd like.  (And we can shorten the function 
name.  *grin*)

For example, can you guess at what:

     print miles_traveled_at_50_mph(4)
     print miles_traveled_at_50_mph(3)
     print miles_traveled_at_50_mph(2)

gives us?  Try it!


Functions let us define a relationship between inputs and outputs.  The 
idea is that we've made our function take in an input, and it can vary its 
answer based on that input.


Our miles_traveled function above changes its answer based on what length 
of time we drive.  But a different function may define an entirely 
different relationship between its input and output.  As another quick 
example:

######
def square(x):
     """Given x, returns the square of x."""
     return x * x
######


or:

######
def height(h):
     """Given someone's height h in feet, tells us if that person is high
     or not."""
     if h < 4:
         return "short"
     elif h < 6:
         return "medium"
     else:
         return "high"
######

We now have two other functions that also take in a single input, but they 
do different things, and even return different kinds of data.  The 
square() example, like the miles_traveled example, takes a number and 
returns another number.  But height() takes a number, and returns a 
string.

In general, functions can take anything as an input, and return anything 
as an output --- I've been using numbers primarily because I'm trying to 
piggyback the knowledge you already should have about math and algebra. 
In my work, all the functions I write use more complex inputs --- and 
usually not numbers --- but the main ideas are the same.


Do you have questions about this so far?


More information about the Tutor mailing list