Parallel Python

Part 1: Functions as Objects

Functional programming is based on treating a function in the same way as you would a variable or object. So, to start, we should first create a function. This will be a simple function that just adds together two numbers. Please type in the Python Console:

In [1]:
def add(x, y):
    """Simple function returns the sum of the arguments"""
    return x + y

This is a very simple function that just returns the sum of its two arguments. Call the function using, e.g.

In [2]:
result = add(3,7)
print(result)
10

which should print out 10.

In functional programming, a function is treated in exactly the same way as a variable or an object. This means that a function can be assigned to a variable, e.g. type

In [3]:
a = add
result = a(3,7)
print(result)
10

This should print 10 again. Here, we have assigned the function add to the variable a. So how does this work?

For variables, you should be comfortable with the idea that a variable refers to a piece of data. For example,

b = 10

would create a piece of data (the integer 10) and will create a variable b which we use to refer to it. When we type

a = b

we are creating a new variable a which points to whatever b was pointing to. Now both a and b contain (or point to) the same data.

For functional programming, the code of a function is also treated like a piece of data. The code

def add(x, y):
    """Simple function returns the sum of the arguments"""
    return x + y

creates a new piece of data (the code to add together x and y), and creates a new name add which points to that code. When we then typed

a = add

we created a new variable a which refers to the same piece of code data that add pointed to. Now both a and add or point to the same data, i.e. the same code that adds together the two arguments (e.g. add(3,7) and a(3,7) will call the same code, and give the same result).

This means that “code of a function” is a type, in the same way that “integer”, “string” and “floating point number” are types.

Properties of a Function

Just as “integer” and “string” have properties, so to does “function”. Type into the Python Console:

add.__[TAB]

(where [TAB] means that you should press the tab key)

This should show something like

add.__call__          add.__dict__          add.__hash__          add.__reduce_ex__
add.__class__         add.__doc__           add.__init__          add.__repr__
add.__closure__       add.__format__        add.__module__        add.__setattr__
add.__code__          add.__get__           add.__name__          add.__sizeof__
add.__defaults__      add.__getattribute__  add.__new__           add.__str__
add.__delattr__       add.__globals__       add.__reduce__        add.__subclasshook__

(exactly what you see will depend on your version of python)

This is the list of properties (functions and variables) of a function. The most interesting variables are __name__ and __doc__. Try typing

In [4]:
print(add.__name__)
print(add.__doc__)
add
Simple function returns the sum of the arguments

From the output, can you guess what these two variables contain?

Functions as Arguments

As well as assigning functions to variables, you can also pass functions as arguments. Type this into the Console:

In [5]:
def call_function(func, arg1, arg2):
    """
    Simple function that calls the function 'func' with  
    arguments 'arg1' and 'arg2', returning the result
    """
    return func(arg1, arg2)

result = call_function(add, 3, 7)
print(result)
10

Can you see why we get this output?

The function call_function takes three arguments. The first is the function to be called. The second two arguments are the arguments that will be passed to that function. The code in call_function simply calls func using the arguments arg1 and arg2. So far, so useless…

However, let us now create another function, called diff:

In [6]:
def diff(x, y):
    """
    Simple function that returns the difference of
    its arguments
    """
    return x-y

and then type

In [7]:
result = call_function(diff, 9, 2)
print(result)
7

What has happened here?

Now we have passed diff to call_function, and so func(arg1,arg2) has used the code contained in diff, e.g. calculating the difference of the two numbers. The result, 7, should be printed.

You are probably now wondering how has this helped? Well, let us now change call_function:

In [8]:
def call_function(func, arg1, arg2):
    """
    Simple function that returns the difference of
    its arguments
    """
    print(f"Calling function {func.__name__} with arguments {arg1} and {arg2}.")
    result = func(arg1, arg2)
    print(f"The result is {result}")
    return result
In [9]:
result = call_function(add, 3, 7)
Calling function add with arguments 3 and 7.
The result is 10

Now try:

In [10]:
result = call_function(diff, 9, 2)
Calling function diff with arguments 9 and 2.
The result is 7

The new call_function is now doing something useful. It is printing out extra information about our functions, and can do that for any function (which accepts two arguments) that we pass. For example, now type

In [11]:
def multiply(x, y):
    """
    Simple function that returns the prodict of the
    two arguments
    """
    return x * y

result = call_function(multiply, 4, 5)
Calling function multiply with arguments 4 and 5.
The result is 20