Tutorial - Routines

The basic building blocks of a program are Functions and Shapes. Although they are fundamentally different in concept and have key differences in syntax ( functions have a value returning mechanism and shapes have a graphical context ) they are quire similar when it comes to most of the coding. So when we want to discuss the similarities we use the common term 'Routine'. The 'Program' block is also similar to a Shape but with a few restrictions on the parameter passing mechanism.

Passing Parameters

In the previous tutorials we showed examples of parameter lists without really explaining how they worked. There are a number of variations on the parameter mechanism but we will start with the simplest scheme.

The simplest way to pass a parameter to a function is know as 'pass by value'. In this scheme the value of the expression to be passed is evaluated and a copy of the result is put in the function's parameter block. This values can be accessed within the function by using the corresponding name on the parameter list. Any changes applied to the parameters in the routine will not affect any values in the calling routine.

A quick bit of terminology. When you define a routine such as :-

Function Number Add(Number x, Number y)
    Begin
	Return x + y;
    End;

The variables x and y are known as the 'formal' parameters. Each parameter must be preceded by the type name even if it is the same type as the previous parameter. No name may occur more than once.

When the routine is called, such as in :-

	res := Add(total, 3 * something);

The expressions "total" and "3 * something" are known as the 'actual' parameters. In our example here we are 'calling by position'. This means that the first actual parameter get passed as the first formal parameter, the second actual as the second formal parameter etc. The names of the formal parameter are local to the routine and have no relationship with any other variable with the same name.

Local Variables

When writing a routine it is useful to have somewhere to store the intermediate results of calculations. These are added to the routine between the function heading and the initial 'Begin'.

Function Number Sum4(Number a, Number b, Number c, Number d)
	Number total1, total2;
    Begin
	total1 := a + b;
	total2 := c + d;
	Return total1 + total2;
    End;

Here 'total1' and 'total2' are a local variables. The type of the variable always appears first. All the following variables, up to the semicolon, will have the same type. You can have as many lines as you like. No variable name may occur twice and may not be the same as the name of a formal parameter. Within the body of the routine a variable can have the value of an expression assigned to it using the 'assignment' operator ':='.

Initialising Local Variables

When a variable is first declared is can be set to an initial value.

	Number total1 = 10, total2 = total1 + 7;

The expression may contain any variable in scope, this include parameters, variables already declared ( and preferably initialised ), and global variables ( covered later ). These expressions are evaluated within the context of the routine and so can contain function calls.

Function Number SumSin(Number a, Number b)
	Number total1 = a, total2 = Sin(total1);
    Begin
	[= . . . rest of code . . . =]
    End;