A LEGO artifact is constructed by selecting certain kinds of bricks (e.g., 2×4 RED) and arranging them in a particular order. A Bricklayer program is constructed in a similar manner by arranging constructs from the language SML in a particular order.
Level 1 programs consist primarily of calls to Bricklayer functions. In fact, except for the first line of code, which is “open Level_1;”, every line of code in a Level 1 program is a Bricklayer function call. The first line, “open Level_1;”, is a declaration. It is this declaration that makes the Bricklayer functions belonging to Level 1 available for use in the rest of the program.
In SML, it is possible for people writing code to create their own functions. These are called user-defined functions, and they are defined using an SML langage construct known as a function declaration. In order to call a function, it must first be declared.
A function declaration consists of the following program pieces:
- The keyword fun which tells the compiler that what follows will be a function declaration.
- The name of the function. A function name typically consists of the concatenation of one or more words. For example, boat or sailBoat would be a good name for a function that, when called, builds a LEGO boat (or sailBoat).
- A list of formal parameters which provides a way for a function call to pass information to the function body.
- The reserved symbol = telling the compiler that what follows is the function body.
- The body of the function.
Concept 10 focuses on learning how to declare functions whose (1) formal parameter lists consist of the unit value (), and (2) bodies consist of expression sequences. In general, we will refer to all functions whose formal parameter list is () as nullary functions.
The example below shows how a sequence of Bricklayer function calls can be transformed into a nullary function declaration. More specifically, program 1 consists of (1) one declaration, (2) four comments, and (3) seventeen Bricklayer Level 2 function calls – one call to build2D, one call to show2D, and the remaining calls to various put2D functions.
Program 2 is obtained by structuring the code in program 1. Specifically, program 2 declares a nullary function whose name is boat. The body of boat is an expression sequence consisting of all the “put2D” function calls which were used in program 1 to create the LEGO boat. IMPORTANT, within an expression sequence a semicolon serves as a separator and not a terminator. What this means is that the symbol before the closing parenthesis “)” of the expression sequence may not be a semicolon.
We will refer to the change from program 1 to program 2 as a program transformation. A program transformation is a rule that tells you how to restructure one program to obtain another program. In this case, program 1 is restructured (aka transformed) to produce program 2.
If we do not look inside the body of boat, then program 2 consists of (1) two declarations, (2) seven comments, (3) two Bricklayer function calls — a call to build2D and a call to show2D, and (4) one call to the user-defined nullary function boat. This view of a program (i.e., the view that does not look inside the bodies of functions) is called the top-level view.
Nullary function declarations provide a basic mechanism for structuring code. The purpose of such structure is to (1) make the code easier to understand, (2) easier to modify, and (3) easier to reuse. For example, in program 1, if I wanted to build the boat twice I would need to repeat all fifteen put function calls. On the other hand, in program 2, if I wanted to build the boat twice I would simply need to simply repeat the function call boat() (nearly doubling the size of the program). Of course, in this example, calling boat would simply overwrite the first boat with the second boat. You would not see anything different. In Concept 11 we will see how an offset can be used to change this. But first we must get comfortable with declaring nullary functions and calling them.
The code for building our LEGO boat can be further structured as shown in program 3. In this example, a nullary function declaration has been written for each part of the boat: the hull, the mast, and the sail. Furthermore, two (or more) different sail functions have been declared: one that builds a white sail, and one that builds a yellow sail. And finally, two boat functions have been declared: one for building a boat with a white sail, and one for building a boat with a yellow sail. Note that (1) the both boat functions have the same hull and mast (an example of reuse), and (2) the bodies of boat1 and boat2 consist exclusively of calls to user-defined functions. The creation of user-defined function declarations whose bodies call other user-defined function declarations is an important step on the way to using the power of the computer.
The top-level view of program 3 consists of (1) seven declarations, (2) sixteen comments, (3) two Bricklayer function calls, and (4) one user-defined nullary function call. Recall in the top-level view you do not look inside the bodies of function declarations.
If we choose, we can declare one additional nullary function which includes all the function calls in program 3. We will call this all encompassing function main. The choice of this name is not arbitrary. It is inspired by languages such as Java and C which make use of a special function, called main, to start off their computations.
Program 4, shown below, is obtained by moving the three top-level function calls of program 3 into a user-defined nullary function called main. As a result, the top-level view of program 4 contains only one function call!
A powerpoint presentation that animates how nullary function declarations and calls can be introduced into a Bricklayer program can be found here.