5.19 Procedure Parameters


5.19 Procedure Parameters

One place where procedure pointers are quite invaluable is in parameter lists. Selecting one of several procedures to call by passing the address of some procedure is not an uncommon operation. Therefore, HLA lets you declare procedure pointers as parameters.

There is nothing special about a procedure parameter declaration. It looks exactly like a procedure variable declaration except it appears within a parameter list rather than within a variable declaration section. The following are some typical procedure prototypes that demonstrate how to declare such parameters:

      procedure p1( procparm: procedure ); @forward;      procedure p2( procparm: procedure( i:int32 ) ); @forward;      procedure p3( val procparm: procedure ); forward; 

The last in the preceding list is identical to the first. It does point out, though, that you generally pass procedural parameters by value. This may seem counterintuitive because procedure pointers are addresses and you will need to pass an address as the actual parameter; however, a pass by reference procedure parameter means something else entirely. Consider the following (legal!) declaration:

      procedure p4( var procPtr:procedure ); @forward; 

This declaration tells HLA that you are passing a procedure variable by reference to p4. The address HLA expects must be the address of a procedure pointer variable, not a procedure.

When passing a procedure pointer by value, you may specify either a procedure variable (whose value HLA passes to the actual procedure) or a procedure pointer constant. A procedure pointer constant consists of the address-of operator ("&") immediately followed by a procedure name. Passing procedure constants is probably the most convenient way to pass procedure parameters. For example, the following calls to the Plot routine might plot out the function passed as a parameter from -2 to +2.

      Plot( &sineFunc );      Plot( &cosFunc );      Plot( &tanFunc ); 

Note that you cannot pass a procedure as a parameter by simply specifying the procedure's name — i.e., "Plot( sineFunc );" will not work. Simply specifying the procedure name doesn't work because HLA will attempt to directly call the procedure whose name you specify (remember, a procedure name inside a parameter list invokes instruction composition). If you did not specify a parameter list, or at least an empty pair of parentheses, after the parameter/procedure's name, HLA generates a syntax error message. Moral of the story: Don't forget to preface procedure parameter constant names with the address-of operator (‘&’).




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net