Board index » delphi » Passing Procedures as Parameters in TP 7.0

Passing Procedures as Parameters in TP 7.0

How do you pass procedures as parameters?  I have tried the folowing
and it has not worked:

procedure CheckArray(TheArray: ArrayType; procedure Checker(var Entry:
ArrayEntryType));
var
   Counter : integer;
begin
   for Counter := 1 to ArraySize do begin
      Checker(TheArray[Counter]);
   end;
end;

I get an 'identifier expected error at the second procedure in the
procedure header?  What am I doing wrong.

Thanks for you help in advance.

Mike Goyetche
mgoye...@mustang.uwo.ca

 

Re:Passing Procedures as Parameters in TP 7.0


Quote
Michael Goyetche wrote:

> How do you pass procedures as parameters?  I have tried the folowing
> and it has not worked:

> procedure CheckArray(TheArray: ArrayType; procedure Checker(var Entry:
> ArrayEntryType));
> var
>    Counter : integer;
> begin
>    for Counter := 1 to ArraySize do begin
>       Checker(TheArray[Counter]);
>    end;
> end;

Try instead:

Type
  CheckerProc = Procedure(Var Entry: ArrayEntryType);
Procedure CheckArray(TheArray: ArrayType; Checker: CheckerProc);
  Var
    Counter: Integer
  Begin
  For Counter := 1 To ArraySize Do
    Checker(TheArray[Counter]);
  End;

In truth, the parameters named in the type declaration are of no
importance; "Procedure" is all that needs to be stated. It is your
responsibility to be sure that Checker is the correct procedure with its
own correct parameters.

AME

Re:Passing Procedures as Parameters in TP 7.0


Quote
Alan M. Evans wrote:

.>
Quote
.> Michael Goyetche wrote:

.> >
.> > How do you pass procedures as parameters?  I have tried the folowing
.> > and it has not worked:
.> >
.> > procedure CheckArray(TheArray: ArrayType; procedure Checker(var Entry:
.> > ArrayEntryType));
.> >
.> Try instead:
.>
.> Type
.>   CheckerProc = Procedure(Var Entry: ArrayEntryType);
.> Procedure CheckArray(TheArray: ArrayType; Checker: CheckerProc);
.>
.> In truth, the parameters named in the type declaration are of no
.> importance; "Procedure" is all that needs to be stated. It is your
.> responsibility to be sure that Checker is the correct procedure with its
.> own correct parameters.
.>

     I could be wrong about this, but it appears that the format that Michael Goyetche used is, in fact,
the method that adheres to (or perhaps near to) the Pascal Standard.  I suspect that the second format
that Alan Evans describes is the Borland "variant" on the Pascal Standard.

     Some Pascal implementations will, in fact, check the parameters of the declared Procedure (or
Function) "variable".  I have found that this layer of compiler-enforced "error checking" (part of
strong typing) has saved me from making many of the silly mistakes that used to plague my Fortran code.

Bob Schor
Pascal Enthusiast

Re:Passing Procedures as Parameters in TP 7.0


Quote
Michael Goyetche wrote:

> How do you pass procedures as parameters?  I have tried the folowing
> and it has not worked:

asing bouwman : try this for a start

Quote
> procedure CheckArray(var TheArray: ArrayType);
> var
>    Counter : integer;
> begin
>    for Counter := 1 to ArraySize do begin
>       Checker(TheArray, counter);
>    end;
> end;

checker should be : procedure checker(var thearray:arraytype, counter:
integer);

this should be working.

Quote
> I get an 'identifier expected error at the second procedure in the
> procedure header?  What am I doing wrong.

> Thanks for you help in advance.

> Mike Goyetche
> mgoye...@mustang.uwo.ca

greetz asing

Re:Passing Procedures as Parameters in TP 7.0


Quote
Bob Schor <bsc...@vms.cis.pitt.edu> wrote:  
> I could be wrong about this, but it appears that the format that Michael
> Goyetche used is, in fact, the method that adheres to (or perhaps near
> to) the Pascal Standard.  I suspect that the second format that Alan
> Evans describes is the Borland "variant" on the Pascal Standard.

Yup.  Exactly right.  One reason why borland's pascal is not standard.
Makes it a pain in the compiler when writing for different compilers such
as THINK Pascal for the Mac, etc.  On a similar note, Borland doesn't
comply to the Object Pascal standard either...
--
Quote
>:-P

Sam Vilain, diony...@sans.vuw.ac.nz

Re:Passing Procedures as Parameters in TP 7.0


"Alan M. Evans" <A.Michael.Ev...@airmail.net> wrote:

Quote
>Michael Goyetche wrote:
>>...
>> procedure CheckArray(TheArray: ArrayType; procedure Checker(var Entry:
>> ArrayEntryType));
>Try instead:
>Type
>  CheckerProc = Procedure(Var Entry: ArrayEntryType);
>Procedure CheckArray(TheArray: ArrayType; Checker: CheckerProc);
>...
>AME

Ok. This will work if you stay in the same segment but will crash if not.
eg. if the checkarray procedure is not in the same unit than the checkerproc.

To avoid crashing, you must use far call for your checkerproc ie. use the {$F+}
and {$F-} compiler directives around your checkerproc procedure.
ie.   {$F+}
       procedure check;
        { var, const, body ... }
      {#F-}
By.

Other Threads