[comp.lang.pascal] procedures as parameters to procedures

VMAN%PACEVM.BITNET@uga.cc.uga.edu ( cliff) (02/01/91)

hi,
 we run vs pascal 2.1 on vm/sp 6.  is it possible to pass as a procedure
 parameter a procedure name with a parameter list?  for example,

program main (input,output);

var word: packed array(1..5) of char;

procedure abc(procedure xyx(var string: packed array(1..5) of char);i:integer);
          .
          .
procedure blah(var string: packed array(1..5) of char);
          .
          .
  begin (* main program *)
          .
          .
   abc(blah(word),3)
  end.

i can pass procedure names without parameter lists as parameters to a
procedure, but i get errors using the above code.  i don't know if this
is a restriction on our particular product or not.  the pascal report
(wirth & jensen) seems to indicate you can do this.  does anyone have an
example of code where this has been successfully done?  any help is
appreciated.

gus@prism.gatech.EDU (gus Baird) (02/01/91)

In article <25782@adm.brl.mil> VMAN%PACEVM.BITNET@uga.cc.uga.edu ( cliff) writes:
 >hi,
 > we run vs pascal 2.1 on vm/sp 6.  is it possible to pass as a procedure
 > parameter a procedure name with a parameter list?  for example,
 >
 >program main (input,output);
 >
 >var word: packed array(1..5) of char;
 >
 >procedure abc(procedure xyx(var string: packed array(1..5) of char);i:integer);
 >          .
 >          .
 >procedure blah(var string: packed array(1..5) of char);
 >          .
 >          .
 >  begin (* main program *)
 >          .
 >          .
 >   abc(blah(word),3)
 >  end.
 >
 >i can pass procedure names without parameter lists as parameters to a
 >procedure, but i get errors using the above code.  i don't know if this
 >is a restriction on our particular product or not.  the pascal report
 >(wirth & jensen) seems to indicate you can do this.  does anyone have an
 >example of code where this has been successfully done?  any help is
 >appreciated.
 
 when you say
 >   abc(blah(word),3)
 you're not passing the procedure blah, you're trying to invoke it - like,
 if it were a function procedure, you'd be passing the RESULT of the 
 function.  when you pass a procedure you pass just the name, it's the
 job of the guy you pass it TO to invoke it with whatever parameters HE
 decides to use.
 why would you want to pass the procedure WITH the parameters?
 if you know the parameter list, YOU can invoke the procedure and
 pass the answer instead of the question...
 anyway, you can't, not this way.  to do that you'd have to give
 abc another parameter, the argument to give to xyz.

 your main problem with a balky compiler may be
 >procedure blah(var string: packed array(1..5) of char);
 where you're trying to declare a procedure parameter of anonymous 
 type.  Pascal requires parameters' types to be named, doesn't it?
 you probably should have said:

 type PAC5 = packed array [1..5] of char;
 ...
 procedure abc(procedure xyz(var P: PAC5); i: integer);
 ...
 procedure blah(var P: PAC5);
 ...
       abc(blah,3)
-- 
gus Baird, College of Computing
Georgia Institute of Technology, Atlanta Georgia, 30332
uucp: ...!{allegra,amd,hplabs,seismo,ut-ngp}!gatech!prism!gus
ARPA: gus@cc.gatech.edu

erin@otago.ac.nz (John Gee) (02/04/91)

In article <25782@adm.brl.mil>, VMAN%PACEVM.BITNET@uga.cc.uga.edu ( cliff) writes:
> hi,
>  we run vs pascal 2.1 on vm/sp 6.  is it possible to pass as a procedure
>  parameter a procedure name with a parameter list?  for example,
> 

IMHO No. Need to pass the parameters separately. Reasoning
for why it won't work follows.

> program main (input,output);
> 
> var word: packed array(1..5) of char;
> 
> procedure abc(procedure xyx(var string: packed array(1..5) of char);i:integer);
>           .

Telling pascal that you will give it a procedure. So that it can
type check, you specify the parameter types that the procedure
takes. (In this case a string)

>           .
> procedure blah(var string: packed array(1..5) of char);
>           .
>           .
>   begin (* main program *)
>           .
>           .
>    abc(blah(word),3)
>   end.
> 

IMHO this tries to pass the result returned by a procedure, as a
procedure parameter - ie it doesn't make sense. Think of putting
a function there instead of a procedure, since you have the
parameters, it seems likely it will evaluate if before passing
it? You need to do something like

   procedure abc (
      procedure xyx (var string: packed array(1..5) of char);
      var word: packed array (1..5) of char);
      i: integer);
   ...
   abc (blah, word, 3);

instead.

-
The views expressed here are my own, and not necessarily those of my employer.

John Gee
Systems Programmer		E-mail:	ERIN@otago.ac.nz (internet)
Computing Services Centre	Phone:	+64 3 4798596
University of Otago		Fax:	+64 3 4798577
PO Box 56
Dunedin				"Program until
NEW ZEALAND			you drop!"

RCIVDAB%HDETUD1.TUDELFT.NL@uga.cc.uga.edu ( Dim Biesheuvel) (02/06/91)

On Tue, 29 Jan 91 11:26:31 EST cliff said:
>
>hi,
> we run vs pascal 2.1 on vm/sp 6.  is it possible to pass as a procedure
> parameter a procedure name with a parameter list?  for example,
>
>program main (input,output);
>
>var word: packed array(1..5) of char;
>
>procedure abc(procedure xyx(var string: packed array(1..5) of char);i:integer);
>          .
>          .
>procedure blah(var string: packed array(1..5) of char);
>          .
>          .
>  begin (* main program *)
>          .
>          .
>   abc(blah(word),3)
>  end.
>
>i can pass procedure names without parameter lists as parameters to a
>procedure, but i get errors using the above code.  i don't know if this
>is a restriction on our particular product or not.  the pascal report
>(wirth & jensen) seems to indicate you can do this.  does anyone have an
>example of code where this has been successfully done?  any help is
>appreciated.
>
Well Cliff, I don't know if others answered your question already, but
I'll try anyway.
You can pass a procedure as a parameter, that's true, but what you are
trying to do is passing a procedure call as a parameter. Now I don't know
what that should mean. If instead of a procedure a function was called
at the activation of abc, yes, that would have a meaning for me.
The function call would yeald a result and that value would be passed to
the corresponding parameter, a value parameter of the same type as the
function.

But what should happen now at the call of abc? Do you want to happen
blah  first? Or do you want to be able to call blah from somewhere in abc.
Well, in that case you just pass blah, without parameters,
in the procedure call of abc, and if you want to
call blah with an argument from your main program, you'll have to
pass that argument as well. So you'll have rewrite abc as follows:

 Type  string5 = packed array (. 1..5 .) of char;

 procedure abc ( procedure xyz ( str : string5 ); pqr : string5;
                 i : integer);
 ...
 begin
     ...
     xyz ( pqr );
     ...
 end;

and abc would be called as

 abc ( blah, word, 3 );

Some remarks:
1. As in a parameter specification only a TypeIdentifier is allowed
   you'll have to introduce a TypeIdentifier for your string variable.
2. A packed variable can only be passed as a value parameter.

I hope this helps.

Dim