Functions

So far my description of modularization may seem hopelessly abstract. What do these modules look like? In Python the simplest form of modularization is to create functions. We have already used of many of Python's built-in functions, e.g. range(), math.sqrt(), and random.randint(), and now it is time to learn to write our own functions.

There is a bit of terminology that will help us talk about functions. We say that "the function range() when called, returns a list specified by its argument". What do the terms called, return and argument refer to? Consider the specific statement,

for i in range(10):

To call a function is to invoke it and we do that by entering its name followed by parentheses. The name of the function being used here is range. To do its job it needs to know how large a range to generate. We tell it by providing the high end of the range, 10, inside the parentheses. This value is the argument to the function. It is sometimes also called the function parameter. The function range will create the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. This is the value it returns or its return value. You can imagine the return value replacing the function call so the code above is equivalent to

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:

So every function has,