[Next] [Prev] [Right] [____] [Up] [Index] [Root]
Procedure Expressions

Procedure Expressions

The main form of the procedure expression is

procedure([~]FORMAL_IDFIER, ..., [~]FORMAL_IDFIER)
    STATEMENTS
end procedure

It creates a procedure whose formal identifiers are the ones listed inside the parentheses. If an identifier is prefixed with a ~ symbol, it is a reference identifier; otherwise it is a value identifier.

The most common use for a procedure expression is to assign it to an identifier immediately, but a procedure can be used as an object in its own right.

The other form of the procedure expression, useful only for adjusting or presetting actual arguments to a previously-existing procedure, is

proc< [~]FORMAL_IDFIER, ..., [~]FORMAL_IDFIER | EXPRESSION >

Here EXPRESSION must be a procedure call, possibly involving the formal identifiers.

Example

Given a sequence Q of rational numbers and a Boolean b at runtime,
this procedure expression, assigned to the identifier intnon, 
will remove from Q all the non-integral entries if b is true, 
and all the integral entries if b is false:

> intnon := procedure(~Q, b) > if b then > Q := [Q[i]: i in [1..#Q] | IsIntegral(Q[i])]; > return; > end if; > Q:=[Q[i]: i in [1..#Q] | not IsIntegral(Q[i])]; > end procedure;

> Q := [6/7, 4, 9, 1/9, 5]; > intnon(~Q, false); > print Q; [ 6/7, 1/9 ]

> PrintLine := procedure(c, n) > print c^n; > end procedure;

> PrintLine("!", 30); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

> PrintStars := proc< n | PrintLine("*", n) >; > PrintStars(53); *****************************************************

[Next] [Prev] [Right] [____] [Up] [Index] [Root]