A-level Computing/AQA/Paper 2/Fundamentals of functional programming/What is functional programming





Functional programming is a declarative programming paradigm, where programs are written as mathematical functions whose order of execution is not solely defined by the programmer. These mathematical functions produce consistent outputs solely dependent on the inputs. Functions can be constructed out of other functions, or call themselves recursively. Functional programming is different from the imperative programming paradigm in several ways:

The outputs of functions only depend on the inputs, and not the changing state of a program, functional programming is stateless. For example, the python program below calls the same function  twice with the same parameters , but different values are returned because the state of the program (here simulated by the variable  ) changes. This wouldn't happen in a functional programming language, as whenever you call a function with the same arguments the same output would be produced.

Functional programming languages avoid side effects. A side effect is where a function modifies something outside its local environment. In the example below the function  changes a global variable   when it is run.

Functional programming also avoids using variables that change at runtime, data structures are considered to be immutable.

Benefits of the functional programming paradigm include:
 * supporting the decomposition and abstraction of computing problems into functions that are made up of other functions which are made of other functions and so on.
 * easier to read and understand code.
 * easier to debug code as each function can be tested separately and does not rely on the state of the machine.
 * lending themselves to running parallel and concurrent progam execution, allowing functional programs to run on multi-processor systems.

Lisp was one of the first functional programming languages, being created in 1958. Several dialects of Lisp are still in common usage including Common Lisp and Scheme. Other functional programming languages include Erlang and Haskell. Whilst imperative programming languages are more commonly used than pure functional languages, it is possible to do functional programming with many modern languages. Other languages that support functional programming include Python, C#, F#, Java, Visual Basic .NET and JavaScript.

Using Haskell for the first time
The majority of the code examples in this chapter will be in Haskell. You can learn more about Haskell over at Learn You a Haskell for Great Good!. To execute the examples shown in this chapter you can download the open source Glasgow Haskell Compiler or GHC. Most examples can be written directly into the command line interface that the GHC gives you, below this is shown with the  symbol. For example:

Later examples might require you to write multiline programs, especially when you need to declare your own functions, to do this you need to open a text editor and save your Haskell file with the  extension. For example:

We can then load this into the GHC by using the  (load) command, followed by the file location and name.

Now we can use the loaded function,, from the command line:

A functional programming language is one where code has no side effects, the program execution is stateless and the outputs of functions only depend on the inputs.


 * easier to debug
 * easier to read and understand
 * can run on parallel systems

We don't know what value  will be at runtime so the   function will output different values depending on the state of the computer.