Functions: A Computer Science Perspective

Introduction

Programming is essentially providing lists of sequential instructions for the computer to perform. Often, we find the situation where we want the same subset of instructions to be run again and again in various areas of our program. Instead of duplicating the instructions (code) each time, we specify that the subset of instructions is defined as a function, and where we need to, we simply call the function in place of where we would have had the duplicate set of instructions. This results in code which is more modular, with reusable code.


Components of a function

A function, in most programming languages, has the following components:

  1. Function identifier. This is the name of the function, which we will use to reference the specific function in other parts of the code.
  2. Parameter List. This is a list of the variables that are sent to the function as inputs.
  3. Instruction Set. This is the main body of the function, where the inputs are usually processed.
  4. Return Statement. The return statement is where the output is returned from the function.

Let's have a quick look at a function written in Python and try identify all of its components. Remember the mathematical function in the previous section defined as:

f(x) = x * 2

Here it is, but written in Python:

def doubled(x):
    answer = x * 2
    return answer

print doubled(4)

Output: 8

In the above example, we can see:

  • The Function Identifier is the name "doubled".
  • The Parameter List only takes a single input variable, "x".
  • The Instruction Set which is made up of the line: answer = x * 2
  • The Return Statement which is return the variable answer

Note: Not all functions will necessarily have a parameter list and a return statement. Take the following function as an example:

import time
def show_date():
    print (time.strftime("%H:%M:%S"))

In the above function, there is an empty parameter list, and no return statement. This function therefore does the same thing each time it is called, without the variability afforded by input parameters. However, notice that it still has an identifier - "show_date" and an instruction set.

Note: It is important to remember that in Python, indentation is very important. You will note that after the function identifier and the empty parameter list, there is a colon (:). This indicates the beginning of the function. Thereafter, the instruction set of the function needs to be indented. Once you are finished the function, you can remove the indentation when you write the next set of instructions after the function.


Functions as input

Remember in the previous section on mathematical functions that a function's output can be used as the input for another function. Let us look at the same example again, where we have two functions:

function_machine_2

  1. f(x) = x^2
  2. g(x) = x + 1

Let's write those two math functions in Python.

def f(x):
    return x * x

def g(x):
    return x + 1

Now, just as we could use the output of one mathematical function as the input of another ( i.e.: g(f(x)) ), we can do the same in Python.

answer = g(f(3))
print answer
[Output: 10]

Exercises

  1. Write a function that takes in a name, and returns the string "Hello {name}".
  2. Write two functions, f(x) that multiples a number by 4, and g(x) which divides a number by four. Then call g(f(5)) and see what answer you get.
  3. Write a function that asks you for your age, and then subtracts that from the current year to give an estimate of the year you were born.

Continue to next section


Comments

comments powered by Disqus