Introduction to Programming with Mathematica, Third Edition

Conventional programming languages like C and Fortran embody a style of programming that has roots in the early days of computing when resource constraints forced programmers to write their code in a step-by-step manner. These procedures, as they came to be known, typically involved certain basic elements: looping over an array, conditional statements that controlled the flow of execution, logical constructs to build up tests, and functions to jump around from one place in a program to another. Although newer languages have introduced many new programming paradigms, procedural programming continues to be used and remains an appropriate style for certain kinds of problems. In this chapter we will look at how procedural programming is used in Mathematica, discuss what types of problems it is most appropriate for, and compare Mathematica's implementation with other languages.
A procedure is a series of instructions that are evaluated in a definite order. The following program is a procedure.
In[1]:= <b class="bold">mat = {{a, b, c}, {d, e, f}, {g, h, k}}; newmat = mat; Do[newmat[[i, j ]] = mat[[j, i]], {i, Length[mat]}, {j, Length[mat]}]; newmat</b> <i class="emphasis">Out[4]</i>= {{a, d, g}, {b, e, h}, {c, f, k}}...