next up previous contents
Next: Nested Routines Up: Functions Previous: Functions

The return Command

In some situations, it is much more convenient to exit a function when a certain condition arises rather than seeping through to the end of the function. To this end, the Darwin language contains the return(exp) command; upon encountering this function within the body of a routine, Darwin immediately returns the result of evaluating expression exp to the place where the function was invoked. Consider the following simple test for primality which returns either true or false.
> IsItPrime := proc (x : real)
>   if (mod(x,2)=0) then           # x is even
>     return(false);
>   else
>     for i from 3 to floor(sqrt(x)) by 2 do
>       if (mod(x, i)=0) then
>         return(false);
>       fi;
>     od;
>   fi;
>   true;
> end;
Here there are three possible exit points from this function. If x is even, then the value of the function becomes false. If x is odd but it has a odd factor between 3 and the floor of its square root, then it also returns false. Only in the case when x is prime does the function seep down to the last statement where its value is assigned true.
> IsItPrime(10039823478323782);         # it's an even number
false
> IsItPrime(3478728737);   # odd but divisible by some number
false
> IsItPrime(101);                       # it's a prime number
true

The value of a function is not restricted to being of type real or boolean. In general, functions may return any data type (including other functions). We could extend our (sophisticated) primality tester to return either true or false and one of the prime factors if it exists.

> IsItPrime := proc (x : posint)
>   if (mod(x,2)=0) then           # x is even
>     return([false, 2]);
>   else
>     for i from 3 to floor(sqrt(x)) by 2 do
>       if (mod(x, i)=0) then
>         return([false, i]);
>       fi;
>     od;
>   fi;
>   [true,1];
> end;
At all three exit points, we return list structures. This need not be the case as Darwin allows at least two other ways of returning multiple values.

The first alternative is to return them as an expression sequence in which case we would re-write the return statements from IsItPrime as follows

> return(false, 2);     # instead of [false, 2]
> return(false, i);     # instead of [false, i]
> true, 1;              # instead of [true, 1]
We advocate against this method as it is sometimes difficult to handle these expression sequences when subsequently passing them to other functions.

The second alternative is to return the values as a data structure. In Darwin, a data structure is any expression which is syntactically a procedure call but does not evaluate (or it is simply not defined). In this way, we have potentially infinitely many different data structures (we deal with this subject extensively in Chapter [*]).

> return(Primality( false, 2 ));    # instead of [false, 2]
> return(Primality( false, i ));    # instead of [false, i]
> return(Primality( true, 1 ));     # instead of [true, 1]


next up previous contents
Next: Nested Routines Up: Functions Previous: Functions
Gaston Gonnet
1998-09-15