[fpc-pascal] arbitrary number of parameters in procedure

Michael Van Canneyt michael at freepascal.org
Wed Aug 31 22:57:30 CEST 2005



On Wed, 31 Aug 2005, Pianoman wrote:

> Hi, I have not well understood the docs concerning tvarrecs. How can I
> create procedure which will accept arbitrary number of parameters?
> To demonstrate what I want:
> procedure addnums(a,b,c,xxxx of any types var result:sometype);
> or writetoscreen(any number of parameters of given type. something like
> write or readln procedures.

Something like this:

Procedure AddNums(Var Result : Double; Args : Array of const);

Var 
  I : Integer;

begin
  Result:=0;
  For I:=0 to High(Args) do // High(args) is the last valid index.
   Case Args[i].Vtype of // args[i] is of type TVarRec 
     VtInteger : Result:=Result+Args[ArgPos].VInteger; 
    // ... etc
   end; 
end;

Call like

  AddNums(MyDouble,[a,b,c,d]);

And you're all done.  

Michael.



More information about the fpc-pascal mailing list