5. Functions


5.1. What are Functions?

In programming, a function is a named sequence of statements that performs a computation. Functions are used to avoid rewriting code everytime a particular task is required to be completed. For example, suppose you have to write a program in which you have to calculate the area of five different fields at different times in the program. Instead of rewriting the code to calculate area each of the five times, we can write a single function to compute area so whenever an area calculation is needed in the program, we simply call the function to do the job.

Each time we call the area function, we would have to pass it the length and width parameters of the field and the computation would be performed. After performing the computation, the function would return the answer to the statement that called it.

The example below shows a function that calculates area based on length and width values that have been passed to it.

def calculate_area(length, width):
   area = length * width
   return area

Points to Note about Functions in Python

  1. Functions are started with ‘def’ keyword which stands for ‘definition’.

  2. Each function has a name. The tern “calculate_area()” is the name of the function. The name is usually followed by opening and closing parenthesis.

  3. The parenthesis is used to store arguments or values from the user that the function needs to perform calculations.

  4. Indentation matters in Python! It determine what is in the function, and when the function ends.

  5. The “return” keyword is used to send results back to the main routine.

  6. To call the above function, use the function name followed by parentheses containing the length and width values. , e.g.,

    calculate_area(300, 245)

  7. To call the function and store the output in a variabe named ‘get_area’, write:

    get_area = calculate_area(300, 245)


5.2. Function Examples

  1. The code below is an example of a function that takes the temperature in Celsius and then prints the results in Fahrenheit results.

def convertTemp(temp):
    freezing = 32

    fahrenheit = (9 * celsius/5) + freezing
    print ("The temperature in Fahrenheit is", fahrenheit, "degrees")
    return fahrenheit

To call the function, type convertTemp and pass in a temperature value, e.g., convertTemp(20)

  1. Function to multiply all the numbers in a list.

mylist = [8, 2, 3, -1, 7]

def multiply(numbers):
   total = 1
   for x in numbers:
       total *= x
   return total

#call the function and submit the list print(multiply(mylist))

  1. The function below takes a number and evaluates it in relation to the number 10.

def number(x):
   if x < 10:
      print (x, "is less than 10")
   elif x > 10:
      print (x, "is greater than 10")
   else:
       print ("your number is 10")
    print ("Goodbye")

Call the function: number (100)

  1. The function below plots a graph. X1 and x2 define the range of the graph over the x axis.

import matplotlib.pyplot as plt
import numpy as np

def createline(x1,x2):
    x = np.linspace(x1,x2)
    y = 2*x+1
    plt.plot(x, y, 'blue', label='y=2x+1')
    plt.title('Graph of y=2x+1')
    plt.xlabel('x', color='#1C2833')
    plt.ylabel('y', color='#1C2833')
    plt.legend(loc='upper left')
    plt.grid()
    plt.show()

Call the function: createline(5,25)


  1. Python function to find the factorial of a number.

def factorial(n):
   fact = 1
   while(n!=0):
      fact *= n
      n = n-1
   print("The factorial is",fact)



Call the function:  factorial(5)

5.3. Python’s In-built Functions

Python has many built in functions. For example, input() is a function which reads and returns the text you type. However, on many occasions, we have to write our own functions.

Type Conversion Functions

int(’ 32’) = 32
int(3.99999) = 3

Math Functions

Here are some examples:

  • math.floor(x) - Returns the floor of x, the largest integer less than or equal to x.

  • math.exp(x) - returns e raised to the power x, where e = 2.718281… is the base of natural logarithms.

  • math.atan(x) - returns the arc tangent of x, in radians.

  • math.cos(x) - returns the cosine of x radians

  • math.pow(x, y) - returns x raised to the power y.

To use the math functions, you have to first import the math library

>>> import math

Next, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation, e.g

>>> math.sqrt(2) / 2.0

0.707106781187

>>> pow(2, 20) #raises 2 to the 20th power.

5.4. Variable-length arguments in Python Functions

Functions do not always have a fixed number of arguments. Sometimes, it is not always possible to know beforehand how many arguments will be passed to the function. For example, in a spatial interpolation exercise, it is possible to estimate the unknown value from three or four or any number of nearby values. In such a case, the function must be designed to handle any number of arguments that will be passed to it.

In Python, we define a variable argument using an asterisk (‘*’) followed by a variable name. By convention, the variable name is called “aargs” but but name can be used. A tuple is used to store the values passed to *args and you can send zero or more arguments to the function. Also, a formal argument cab be placed before the *args variable but not after it.

The following example demonstrates the usage of a function that takes variable length arguments.

def add_num(*args):
    sum = 0
    for num in args:
       sum += num
    return sum

    result = add_num(1, 2, 3)
    print('Sum is', result)

    result = add_num(10, 20, 30, 40)
    print('Sum is', result)

    result = add_num(5, 6, 7, 8, 9)
    print('Sum is', result)

5.5. Python Docstrings

Python docstrings are strings used to document code. They are placed right after the definition of a function, as in the example below.

def square(n):
    '''Takes in a number n, returns the square of n'''
    return n**2

print(square.__doc__)

Here is the docstrings for the built-in function int() function in Python. Run it and note the results. Click on this hyperlink to learn more about Python docstrings.

print(int.__doc__)

5.6. Arcpy Functions

Arcpy has many functions which are used to support ArcGIS workflows from a Python perspective. Take a look at this list of ArcPy functions

When working with arcpy’s functions, we must first import the arcpy library into our development environment then call the function. To call the function, we write arcpy followed by the name of the function and any arguments that the function requires, for example, arcpy.function_name(arguments).

In the code sample below, we are using ArcPy’s ListField() function to print out a list of the fieldnames in a shapefile’s attribute table.

#Listfields function

import arcpy
arcpy.env.workspace = "c:/data"
fieldlist = arcpy.ListFields("roads.shp", "", "String")

for field in fieldlist:
   print (field.name)

In the script below, Arcpy’s ListRaster function is being used to print out a list of raster files in a folder. Run the using your own data. Study each line of the code to understand the script in its entirety.

import arcpy

# Set the workspace that contains the rasters
arcpy.env.workspace = "C:/data/rasters"

# Use the ListRaster function to return a list of rasters .
rasters = arcpy.ListRasters()

# print name of feature class
for rst in rasters:
   print (str(rst))

#Get a list of field names in the roads shapefile

  import arcpy
  arcpy.env.workspace = "c:/data"

  fieldlist = arcpy.ListFields("roads.shp")
      for field in fieldlist:
           print (field.name, field.type, field.length)



#Get a List of all the Feature Classes in a Directory
   import arcpy
   arcpy.env.workspace = "C:/data"

   fcList = arcpy.ListFeatureClasses ()
   for fc in fcList:
      print (fc)




#Get a List of Tables in a geodatabase
import arcpy

# Set the current workspace
arcpy.env.workspace = "c:/data/mydata.gdb"

# Get and print a list of tables
tables = arcpy.ListTables()
for table in tables:
    print(table)

5.7. Resources


5.8. Exercises

  1. Write a Python function that squares each value in a list that is passed to it. The results should be passed to the calling statement and printed out.

    my_list = [11,14,15,100, 34, 67, 89, 90, 98]

  2. Define a function that accepts radius and returns the area of a circle.

  3. Define a function that accepts two numbers as arguments and returns the first number raised to the power of the second number.

  4. Rewrite the spatial interpolation program you wrote in the second lab as a function.

  5. Rewrite the gravity model that appears in geography textbooks as function.

  6. The script sbelow uses arcpy’s buffer function to buffer a shapefile. Run the script using your own data. Comment it to show your understanding of the function.

1import arcpy
2
3try:
4  infc = r"C:\mydata\California\California.shp"
5  outfc = r"C:\mydata\California\CA_Buffered.shp"
6
7  bufferDistance = 2000
8
9  arcpy.Buffer_analysis(infc, outfc  bufferDistance", "", "", "ALL")
  1. The script below is another arcpy buffer script to buffer multiple feature classes. . Run the script using your own data. Comment it to show your understanding of the script.

 1import arcpy
 2
 3#Set geoprocessing environments
 4arcpy.env.workspace = r"C:\mydata\California"
 5arcpy.env.overwriteOutput = True
 6
 7#Create list of feature classes
 8fclist = arcpy.ListFeatureClasses()
 9
10
11#Provide a list of distances to be used for buffering
12distances = [1000, 5000, 8000]
13
14
15# Create a loop to buffer the list of feature classes
16
17for distance in distances:
18    for fc in fclist:
19         arcpy.Buffer_analysis(fc, "_Buffer" + str(distance),  "distance", "", "", "ALL")