Common pro­gram­ming languages usually allow for several pro­gram­ming paradigms, whereby a rough dis­tinc­tion is made between de­clar­at­ive and im­per­at­ive pro­gram­ming. To put it simply, such paradigms form the basic approach when pro­gram­ming software. A sub-form of the de­clar­at­ive approach is so-called func­tion­al pro­gram­ming, which is pre­dom­in­antly used in the de­vel­op­ment of the following programs or codes:

  • Technical and math­em­at­ic­al ap­plic­a­tions
  • Ar­ti­fi­cial in­tel­li­gence (AI)
  • Compilers and parsers
  • Al­gorithms

What makes the pro­gram­ming approach or func­tion­al pro­gram­ming languages so in­ter­est­ing for this type of computer ap­plic­a­tion? And how does it differ from other concepts such as object-oriented pro­gram­ming?

What is func­tion­al pro­gram­ming?

As the name suggests, the focus of the func­tion­al approach to pro­gram­ming is on functions. With a func­tion­al program, all elements can be con­sidered a function, and the code can be executed with con­cat­en­ated function calls. Con­versely, there are no in­de­pend­ent as­sign­ments of values. A function is best envisaged as a special variant of a sub­pro­gram. This is reusable and, unlike a procedure, directly returns a result.

There are, of course, functions in many higher-level pro­gram­ming languages which are defined and then applied, so this is not a special feature of func­tion­al pro­gram­ming. The thing that makes the func­tion­al approach so important for computer science, and at the same time ap­pro­pri­ate for a wide range of ap­plic­a­tions, is the fact that functions within this pro­gram­ming paradigm can take on different "forms". This way they can be linked together just like data. Fur­ther­more, use as a parametre as well as use as a function result are possible. This special handling of functions allows pro­gram­mers to implement and process complex com­pu­ta­tion­al tasks (in par­tic­u­lar those of a symbolic nature).

Why func­tion­al pro­gram­ming is more relevant today than ever

Although the roots of func­tion­al pro­gram­ming date back to the 1930s (as part of basic math­em­at­ic­al research), the func­tion­al approach continues to be very popular, es­pe­cially within technical and math­em­at­ic­al fields. There are several reasons for this:

  • Extensive pos­sib­il­it­ies for algebraic program trans­form­a­tion
  • Extensive pos­sib­il­it­ies for algebraic program synthesis
  • Simple semantic analysis pos­sib­il­it­ies thanks to the re­nun­ci­ation of "inner states in the cal­cu­la­tion process" and "by-effects".
  • Elim­in­a­tion of internal con­di­tions: unlike im­per­at­ive pro­gram­ming, no internal states of a cal­cu­la­tion process are required.
  • Re­nun­ci­ation of side effects: the state changes which go hand-in-hand with the internal states, the so-called by-effects, can also be dispensed within func­tion­al tasks.

Func­tion­al pro­gram­ming offers a high degree of ab­strac­tion as it is based on the math­em­at­ic­al concept and principle of function. Clean ap­plic­a­tion of this type of pro­gram­ming leads to very precise code. Many small, reusable and highly spe­cial­ised units, as well as the functions, create a program to solve a much larger task.

Therefore, there are many practical reasons why func­tion­al pro­gram­ming and the func­tion­al pro­gram­ming languages working with this principle are very popular in computer science, in par­tic­u­lar, when it comes to complex math­em­at­ic­al problems and al­gorithms. At the same time, the very specific ap­plic­a­tion areas ensure func­tion­al pro­gram­ming languages play something of a niche role.

At a glance: The most important func­tion­al pro­gram­ming languages

The most important pro­gram­ming languages based on the func­tion­al approach include the following:

  • LISP
  • ML
  • Haskell
  • OCaml
  • F#
  • Erlang
  • Clojure
  • Scala

Fur­ther­more, there are well-known pro­gram­ming languages that allow func­tion­al pro­gram­ming as one of several possible paradigms:

  • Perl
  • Ruby
  • Visual Basic .NET
  • Dylan
  • ECMAScript

Ad­vant­ages and dis­ad­vant­ages of func­tion­al pro­gram­ming

Ad­vant­ages Dis­ad­vant­ages
Programs are stateless Data (e.g. variables) cannot be changed
Well-suited for par­al­lel­isa­tion Retrieval of large amounts of data not possible ef­fi­ciently
Easily testable code Not re­com­men­ded for con­nec­tions to databases and servers
Easily veri­fi­able code, even stateless functions can be verified Not suitable for many re­cur­sions of the same stack
Can be combined with im­per­at­ive, object-oriented pro­gram­ming Recursive pro­gram­ming can lead to serious errors
More accurate, shorter code Not suitable for all tasks

The table provides an overview to determine whether the func­tion­al paradigm is the right approach for your software pro­gram­ming project or not. The decision in favour of a pro­gram­ming style is often strongly dependent on the personal pref­er­ences of the developer. For this reason, for example, object-oriented pro­gram­ming is a popular al­tern­at­ive to the func­tion­al approach. The two ap­proaches should, therefore, be briefly compared in the following - including a final practical example.

Trend or no trend? Object-oriented and func­tion­al pro­gram­ming in cross-com­par­is­on

Just as in fashion, there are various trends in pro­gram­ming. For quite some time now, object-oriented pro­gram­ming has been on-trend. It’s par­tic­u­larly popular in the de­vel­op­ment of web ap­plic­a­tions and computer games. In com­par­is­on to func­tion­al pro­gram­ming, one does not describe the in­di­vidu­al elements as a function but as objects and classes when using this approach. In com­bin­a­tion with an in­her­it­ance system, this offers the advantage that all com­pon­ents can be easily reused and expanded at any time. Func­tion­al code, on the other hand, is shorter, clearer, and preferred where testable and veri­fi­able code is needed.

In­cid­ent­ally, the choice between object-oriented and func­tion­al pro­gram­ming does not ne­ces­sar­ily have to be made. Many modern pro­gram­ming languages support working with both pro­gram­ming styles. This way, they can be easily combined and de­velopers benefit from the merits of both paradigms.

Func­tion­al pro­gram­ming using the example of a parser

Parsers are software com­pon­ents that are often in­dis­pens­able as su­per­vis­ory mech­an­isms for compilers to translate pro­gram­ming language into machine language.

A parser can generally be im­ple­men­ted on the basis of various pro­gram­ming paradigms, for example, with an object-oriented language. However, the func­tion­al approach offers a set of useful ad­vant­ages with regard to a parser’s code design:

  • There are no global and change­able variables. Ac­cord­ingly, no pro­gram­ming error can result from the so-called "mutable global state”, as can be the case with object-oriented projects. This benefits a parser as a central element of the program.
  • Thanks to higher-order functions and man­age­able program code, larger data col­lec­tions can be easily handled. This is very useful to a parser, which processes large amounts of data.
  • A parser is a program element that is run fre­quently. It helps the overall program, provided that this central element is precisely pro­grammed and runs ef­fi­ciently, as is the case with func­tion­al pro­gram­ming.
  • An error in the parsing process is usually fatal and must be avoided at all costs. While a program is running, however, there will certainly be a lot of semantic de­pend­en­cies that can often lead to serious errors. When im­ple­men­ted properly, func­tion­al pro­gram­ming can help to minimise or even prevent such serious errors in execution.
Go to Main Menu