6. Functions & Includes

These are the two major ways of saving yourself programming time, by reusing the code you or other people have already written (and hopefully tested). Small functions will make, while some judiciously-chosen includes will save you hours and hours of rewriting.

6.1 Functions

Functions are probably the most useful way of saving yourself time. Most programs involve repeatedly performing some simple operation. Putting this calculation into a function and then calling the function results in code that is easier to read, less buggy (you only need to debug it once) and faster to write. A function looks like this:
  •  def functionname(argument1, argument2):
         #code
         return returnvalue

6.1.1. Return values

The primary way of getting data out of a function is with the return value. The return value can be an array, which allows you to return multiple things to the main program.

Return values are optional; a function's output may not need any further processing (for example, if you had a function which merely printed out a warning message, you wouldn't need to do any further calculation).

You can accomplish the same task with global variables, but this is messier.

Here's an example of getting multiple return values:
  • (five, twentyfive, onetwentyfive) = listexample(5) # gives 5, 25, 125 to the corresponding variables 
    def listexample(input):
        num = input
        numsquared = num*input
        numcubed = numsquared*input
        return num, numsquared, numcubed
>

6.1.2. Arguments

The primary way of getting input to a function is with arguments. Like return values, these are not required (although most useful functions, like most useful programs, take some input and produce some output). In the example above, the variable 'input' (with the given value 5) is the argument.

You can make a function more flexible by assigning default values to arguments:
  •  a = 2
     b = 3
     print(sum(a,b)) # prints 5
     print(sum(a,b,10)) # prints 15
     def sum(a, b, c=0):
         return (a+b+c)
Note that specifying a default value for c allows you to skip it when it's not needed.

6.1.3. Global variables

Python is much looser on 'scope' than many other programming languages. That said, to modify one of your program's variables inside a function, you will need to use the 'global' operator:

  • fruit = "apples"
    def gotomarket():
        global fruit
        fruit = "oranges"
        return
    gotomarket() # converts apples into oranges
    print("I like to eat", fruit, "!") #prints "I like to eat oranges!"

In the above program, fruit (originally defined as 'apples') is converted to 'oranges' by the function 'gotomarket()', and then printed.

As an aside, global variables are deprecated in computer science. According to most modern computer scientists, as well as in my own experience, cleaner, less-buggy code results from strict scoping-- i.e., only passing data between the main program and subroutines via return values and arguments:
  •  def gotomarket(foodin): #passes data to the function via an argument
         foodout = "oranges"
         return foodout # passes data back to the main program via a return value
     fruit = "apples"
     fruit = gotomarket(fruit) # converts apples into oranges
     print("I like to eat ", fruit, "!" #prints "I like to eat oranges!")
But it's a matter of style; some people are much more comfortable with global variables.

6.1.4. Recursion

A neat trick for making functions simpler (or at least, shorter!) is to include the function inside itself. This is probably best explained by an example:
  • def fact(n):
        if (n<=1):
            return 1
        else:
            return n*fact(n-1)
When you call fact(3), the following happens:
  •  fact(3)?
      return 3* fact(2)
       fact(2)?
        return 2*fact(1)
         fact(1)?
          return 1
     return 3*2*1 = 6
When writing recursive functions, it is very important to ensure that the recursion ends somewhere! Note that although the function keeps calling itself over and over, it ends once it reaches the number 1.

Python does have a built-in recursion limit (1000 recursions) but this may be modified via the sys module.

6.2. Includes


One of Python's strengths is its vast quantity of community-created content. available to be inserted into your code in the form of modules. These are typically added using one of the following lines:
  • from module import *
or
  • import module
In general it is better to use the second form, and best to only import those methods you need. For example:
  • from math import cos
Importing only those functions you need will result in faster, smaller and less buggy code.

The functions you've imported can be used like regular functions:
  • cos(0) # 1

6.2.1. Some common includes

  • math - Mathematical operations
  • sys - python built-in system settings and variables (such as the recursion limit, program arguments, etc)
  • numpy - numeric routines
  • scipy - scientific routines
  • RS - R / S-Plus bindings for Python
  • Matplotlib - Plotting functions
  • getopt - advanced command-line argument processing
  • os - Operating system commands (path manipulation, etc.)
  • random - Better way to generate random numbers (random.random) [can also be done with os.urandom()]
  • time - Time functions (get current time, calculate time later, etc)
  • datetime - Date & time functions
  • pickle - Data pickling

Up to Index
Previous: 5. Data types | Next: 7. Miscellanea