# Functional vs. Procedural Programming Language

Mathematica and Maple are both computer algebra systems that contain underlying progamming languages. The languages are quite different from one another. A basic difference is the difference between procedural and functional styles.

### Procedural Programming

The traditional programming languages, which students usually learn first, are procedural languages. These include C, C++, Fortran, Pascal, and Basic.

A procedural program is written as a list of instructions, telling the computer, step-by-step, what to do: Open a file, read a number, multiply by 4, display something. Program units include the main or program block, subroutines, functions, procedures; file scoping; includes/modules; libraries.

Procedural programming is fine for small projects. It is the most natural way to tell a computer what to do, and the computer processor's own language, machine code, is procedural, so the translation of the procedural high-level language into machine code is straightforward and efficient. What is more, procedural programming has a built-in way of splitting big lists of instructions into smaller lists: the function.

Overall, Maple is a procedural language. It has only a few of the basic functional programming constructs that characterize a functional language and its pattern-matching capabilities are very limited. See a Maple programming tutorial:

### Functional Programming

Functional programming languages emphasize rules and pattern-matching. While they appear non-intuitive to those who have only experienced procedural languages, they provide succinct and natural programming structures for those who gain some experience. Functional programming is particularly useful for mathematical work, where the notion of ``function'' is already a well established concept.

Mathematica is, in essence, a functional programming language that is implemented as a term rewriting system with very extensive pattern-matching capabilities; one can program in Mathematica using the procedural style of programming (e.g., using Do loops) but it is definitely preferable to adopt the functional or rule-based style. See a Mathematica programming tutorial:

• books Introduction to Programming with Mathematica, Second Edition, Richard J. Gaylord, Samuel N. Kamin, and Paul R. Wellin; Mastering Mathematica: Programming Methods and Applications, John W. Gray; Programming in Mathematica Second Edition, Roman Maeder.

It was true that functional programs generally ran more slowly than procedural programs. However, runtime is not the whole story in terms of efficiency. The time it takes to develop code, and even more importantly, to modify programs, is substantially faster for functional programs than for procedural programs. This is important for protyping and carrying out exploratory research. Furthermore, for machine-precision calculations on lists and matrices and tensors -- the backbone of industrail-strength number crunching! -- the gap between the speeds of Matlab and Maple and Mathematica have been programmed away. Mathematica uses packed array technology to give the twin benefits of increased speed (the speed of large matrix calculations can gain an order of magnitude!) and increased capacity, since larger matrices can be packed into available memory.

An additional factor that needs to be taken into account when evaluating the efficiency of a language for computer simulation work is related to what computing tasks are going to be carried out. Most simulation studies include not only creating and running a program but also analyzing the results both visually and numerically (as well as writing up the study both for publication and presentation). There is a major benefit to using a computer algebra system such as Mathematica, Maple, Macsyma, or Axiom which provides a unified computing environment for carrying out all of these tasks.

(From a Mathematica marketing blurb): Functional programming: Mathematica's functional programming constructs, Map[] and Apply[] allow you to do many things in one line that would normally take several loops in other languages. Remember, ``if you aren't programming functionally, you're programming dysfunctionally''! When you add Fold[] and Nest[] into the mix, you can do some pretty powerful things in a couple of lines...