next up previous contents
Next: Commenting a Procedure: description Up: Procedures Previous: Procedures

   
The proc Command

A procedure is a programming construct which groups together a set of commands to form a single statement. Once grouped this procedure can be called with different arguments. In Darwin, procedures are data structures (we will come back to this topic later) and typically have a name in the same fashion as variables and other data structures have names. This name is used to invoke or execute the routine. The built-in commands proc and end are used to define procedures as follows:

> FirstTry := proc() 
>  print('We can put as many statements as we\''d like in here');
>  print('Now preparing for departure...');
> end;
The body of the procedure consists of two print statements. To invoke the procedure, we type:
> FirstTry();
We can put as many statements as we'd like in here
Now preparing for departure...
The parenthesis surround the formal arguments (or formal parameters) to a procedure. In the above case, there are no arguments. Arguments allow information to be passed to a procedure so that it may be used by the body. This data may be of any type. We can now write a procedure to find the average value in a set by passing an argument of type set.
> SetAverage := proc(s : set)
>   description 'This procedure takes a set as an argument and
>                calculates the average value in the set';
>   total := 0:
>   for i from 1 to length(s) do
>     total := total + s[i];
>   od;
>   lprint('The average is ', total/length(s));
> end:
In procedure SetAverage, there is one argument s of type set. When we invoke SetAverage accompanied with the argument, Darwin checks to see if the data we have passed to it are of the correct type. The invocations
> SetAverage({1, 5, 10, 15});
The average is  7.7500
> SetAverage({66});
The average is  66
are perfectly okay although
> SetAverage( [1, 5, 10, 15] );
SetAverage expects a 1st argument, s:set, found: [1, 5, 10, 15]
Error, invalid arguments
> SetAverage( 'infiltrate and destroy' );
SetAverage expects a 1st argument, s:set, found: infiltrate and destroy
Error, invalid arguments
both result in errors. Specifying the type of the parameter in the proc declaration allows Darwin to perform type checking. Computer science wisdom says that programs become easier to understand and debug when more type checking is included in the program. However, specifying the type of arguments is optional in Darwin and if we had chosen to write the first line of SetAverage as
> SetAverage := proc (  )            # no type declaration
...
no such type checking would be carried out. Any argument passed to the routine would be accepted although this may cause errors within the body of the procedure. We can extend SetAverage to perform stricter type checking.
> SetAverage_2 := proc(s : set(real))
>   description 'This procedure takes a set of real values as an 
>       argument and calculates the average value in the set';
>   total := 0:
>   for i from 1 to length(s) do
>     total := total + s[i];
>   od:
>   lprint('The average is ', total/length(s));
> end:
Now, whenever SetAverage_2 is invoked, each element of the set s is checked whether it is of type real.
> SetAverage( {'I', 'am', 'sneaky'});     # The first proc doesn't
                                          # stop this.  The error
                                          # occurs in the body.
Error, (in SetAverage) invalid addition of text

                                          # the second does stop it
> SetAverage_2( {'but', 'I', 'fail', 'anyway'});  
SetAverage_2 expects a 1st argument, s:set(real), 
found: {I,anyway,but,fail} Error, invalid arguments
In general, each nested level of a parameter can be checked by nesting the specified type inside of parenthesises.
> TypeCheckingProcedure := proc (weird : list(list(integer)))
>      print('This data is A-OK', weird);
> end:
> TypeCheckingProcedure([[5, 6, 7], [8, 9, 10]]);
This data is A-OK
  5  6  7
  8  9 10


next up previous contents
Next: Commenting a Procedure: description Up: Procedures Previous: Procedures
Gaston Gonnet
1998-09-15