
132 Pascal 4.0 User’s Guide
6
Procedures and Functions as Parameters
It is probably clearer to pass a pointer to a procedure than to pass the
procedure name itself. See “Procedure Calls: Pascal–C” on page 117.
A procedure or function passed as an argument is associated with a static link
to its lexical parent’s activation record. When an outer block procedure or
function is passed as an argument, Pascal passes a null pointer in the position
normally occupied by the passed routine’s static link. So that procedures and
The Pascal main program,
ProcParMain.p, which calls the
C procedure, proc_c, passing it
the address of the Pascal
procedure, proc_pas. The C
procedure assigns a value to the
string s, and calls the procedure
whose pointer it just received.
Then the Pascal procedure,
proc_pas, writes a literal
constant and the string it just
received.
program ProcParMain;
type
{ Declare a procedure pointer type. }
proc_ptr = ^procedure(var s: string; i: integer);
{Declare an external C procedure which takes a procedure argument.}
procedure proc_c(p: proc_ptr); external c;
procedure proc_pas(var cstr: string; strlen: integer);
var
i: integer;
begin
write('Hello from PROC_PASCAL: ');
for i := 1 to strlen do
write(cstr[i])
writeln;
end; { proc_pas }
begin
{ Call the C routine. }
proc_c(addr(proc_pas))
end. { ProcParMain }
The commands to compile and
execute ProcPar.c and
ProcParMain.p
hostname% cc -c ProcPar.c
hostname% pc ProcPar.o ProcParMain.p
hostname% a.out
Hello from PROC_PASCAL: Called from C
Kommentare zu diesen Handbüchern