Chapter 12

Chapter 12

 

On Functional Programming

   A subprogram is a program sub-unit that has usually been given a name. Writing a subprogram reduces the size of our code because otherwise it would lead to repetition of program statements. More importantly, it gives us an abstract feel for the task it performs. Additionally, once we have identified a task, abstracted it away, and have written the subprogram, we can use the task without bothering about how the task is actually implemented. We can also change the implementation of the task that is contained in the subprogram without affecting the rest of the program.

Thus, a subprogram takes a collection of programming language statements and makes them reusable. Usually, the collection is given a name although it is possible to write nameless or anonymous subprograms. Subprograms are usually parameterized so that an invocation of a subprogram may be slightly different from another. This individuation comes by due to the use of parameters or arguments. Certain variables in a subprogram can be considered parameters so that an invocation identifies the real values of these parameters. When a subprogram is defined, the parameters used inside the subprogram definition are called formal parameters. When a subprogram is called, the parameters supplied in the call are called actual parameters.

There are two usual ways of writing subprograms: procedures and functions. A procedure is a collection of statements that performs the computation needed to perform a task. The computation is performed when the procedure is called. A procedure can have side effects such as printing and setting of variables that are visible to the procedure.

A function is similar to a procedure, but is modeled after mathematical functions. For example, the mathematical function f(x,y) has a name f, and two formal parameters x and y. When we invoke the function f with two parameters, say  and , where  and  have been assigned some values, it performs a computation with these two values and gives the result as its output. The input variables  and  are not changed by a call to the function f.

In a programming language that supports ideal functions, a function does not produce any side effects, that is, neither does it change any of the parameters sent to it nor does it change any variables declared outside the function. A function works by returning a value as the result of the computation it performs. A procedure does not have to return a value and can perform all its computation as side effects.

In Perl, every subprogram we write is a function in the sense that the subprogram returns a value from it. The value returned is the value returned by the last expression that is executed before the function terminates or a value that is explicitly returned by using a return statement. However, the subprogram can have side effects. So, in reality, a Perl subprogram is both a function and a procedure. A Perl function can alter the values of global variables that can be seen from inside the function, and it can have other side effects such as printing of text and values of variables. Perl functions can even change the values of parameters sent to them, especially if the parameters are sent by reference, as we will see later. We will use the terms function and subprogram interchangeably.

In programming languages such as Lisp and ML, functions are first-class objects. This means that functions are just like any other data structures that these languages support. In other words, they are not different from scalars, arrays, and hashes, and can be manipulated as such. In most programming languages, this is not the case. Functions are treated differently in these languages. When functions are treated as first-class objects, it is possible to write programs that are much more sophisticated compared to languages that do not allow first-class functions. Perl functions are not quite first-class objects, but are quite powerful as will soon see.