Functional, or applicative, programming languages are based on the application of mathematical functions to computer programming. A function is a statement that returns a single value based on the arguments, or inputs, it is passed. Functional programming languages evaluate expressions that are formed using functions.
To understand functional programming languages it is necessary to contrast them with imperative programming languages. Imperative programming languages are based on the architecture of the computer. With imperative languages a programmer defines a set of executable instructions and the exact sequence in which those instructions are executed. The computer simply obeys the instructions in the sequence it is told and stores the result as a variable in its memory. These elementary instructions are repeated, new values are stored in variables, and these variables are assigned to memory locations until the problem is solved. In contrast, functional programming languages define only the problem to be solved not the instructions to solve it. The computer determines the necessary steps to solve the problem and returns a single value. Functional programming languages emphasize the evaluation of expressions, rather than the step-by-step execution of program instructions. This is possible because functional languages are based on the evaluation of mathematical functions.
What Is a Function?
A function takes zero or more arguments and computes a value from those arguments. For example:
(+ a b)
This +, or addition, function has two arguments: a and b. When passed numeric values for a and b, the + function will return a single numeric value. The arguments are said to be in the domain set of the function, and the value returned is in the range set of the function. A function is a rule for mapping, or associating, members of the domain set to those of the range set.
The power of functional programming languages comes from the ability to combine functions. A function can be an argument for another function. For example:
(* (+ a b) (- c d))
The *, or multiplication, function is passed two arguments. The first is an addition function, the second is a subtraction function.
Features of Functional Programming Languages
In the function (+ a b), a and b are values. The result depends only on what arguments are passed into the function. Imperative languages have variables not values. These variables are stored in the computer's memory and their state changes depending upon other instructions executed in the program. For example, to add numbers 1 through 10 in an imperative language, a procedure would have to be written with a variable that held the answer from each previous addition. The variable's state would have to be updated each time through an assignment statement when the next number was added to the previous sum. The procedure would have to be repeated until all the numbers were added.
In a functional language, variables are not necessary and no updating takes place. The numbers 1 through 10 are added and return a single value. This addition function would only have to be executed once. This simplicity is possible because functional programming languages do not use variables, assignment statements, or iteration as do imperative languages. Instead functional languages use three main components:
a set of data objects
a set of built-in functions
a set of functional forms
Data objects are the members of the function's domain and range sets. In functional programming languages a single structure, such as an array or list, is used for data objects. Built-in functions are used to manipulate the data objects. For example, in the functional programming language LISP, CONS is a function that appends an element to a list.
(CONS (A) (B C)) = (A B C)
Functional forms are mechanisms for building new functions. For example, the functional form "?", or composition, takes two functions and returns the result of the application of the first function to the result of the application of the second function.
Another feature of pure functional languages is the lack of side effects. A side effect is a change in state caused by the execution of an instruction. In the imperative language example, each time a number was added to the previous sum the state of the variable was changed. This was a side effect of each execution of the sum procedure. A pure functional language does not have such side effects because the arguments passed to the function do not change. Some functional languages do allow side effects and are, therefore, not considered purely functional.
Advantages and Disadvantages
Proponents of functional languages believe that the dependence of imperative languages on the computer hardware is a flaw that functional programming languages can overcome. A functional-language programmer can concentrate on understanding and describing a problem rather than spending time and effort in providing a computer with step-by-step instructions for solving a problem. Additionally, functional languages do not constantly modify the state of variables resulting in side effects. The constant modification of variables in imperative languages makes imperative programs hard to understand and to correct. Additionally, the lack of variables, assignment statements, and repetition in functional languages allows for shorter programs written more quickly and more concisely; and, as a result, such programs can be more easily maintained.
On the other hand, functional programs are harder to implement efficiently than those written in imperative languages. Since functional languages are not based on the computer's architecture efficiency suffers, particularly in attempts to solve large problems. The high level of extraction found in functional languages can create poor performance. Until computers can more efficiently process functional languages, imperative languages will remain widely used.
This is the complete article, containing 912 words
(approx. 3 pages at 300 words per page).