Does any Pascal dialect allow a variable number of arguments?
This is a question for old programmers.
A few years ago I came across a Pascal dialect that allowed a variable number of arguments through some kind of extension.
Does anyone know of the current Pascal dialect that allows a variable number of arguments?
Given that Pascal is not as popular as he used to be, I would not be surprised if the answer is no.
By the way, it's more correct, isn't it, to say a variable number of arguments, not parameters?
source to share
Not. The answer is based on the Pascal dialects I have used; others may be different.
The reason is that Pascal casts the arguments to the stack frame one by one, so all arguments are available at a fixed offset from the stack pointer. C, by comparison, pushes the arguments in reverse order, so certain parameters have a fixed offset, and you can access the "extra" arguments using pointer arithmetic. I'll try some ASCII art:
Pascal C
---------------------
| extra arg |
--------------------- ---------------------
| 1st param | | 3rd param |
--------------------- ---------------------
| 2nd param | | 2nd param |
--------------------- ---------------------
SP -> | 3rd param | | 1st param |
--------------------- ---------------------
As for the parameter versus argument: as I found out, the function (method) defines its parameters, the caller passes the arguments. This definition came from, I suppose, from the Fortran manual, so it should give you an idea of how old I am :-)
source to share
You can use optional arguments with delphi to get the same effect:
procedure Proc(const A: Integer; const B: Integer = 15);
Proc(10); // B = 15
Proc(20,30);
Or overloaded methods:
procedure Proc(const A: Integer); overload;
procedure Proc(const A,B: Integer); overload;
Proc(10); // Variant 1
Proc(20,30); // Variant 2
Or you can use a variable array for the parameters:
procedure Message(const AMessage: string; const AArgs: array of const);
Message('Hello %s', [Name]);
Message('%s %s', [Greeting, Name]);
source to share
You are probably thinking of a library available for Turbo Pascal where they had such a hack. My syntax rusts a little for objects and descends from it.
type
TValue = object;
TInteger = object(TValue)
Value : Integer;
end
TString = object(TValue)
Value : String;
end
TParam = record
Value : TValue;
Param : TParam;
end;
TValue = object;
{ Definition of Function }
function Test (Arg : TParam);
{ Usage }
var
I : TInteger;
S : TString;
Test (TParam (I, TParam (S, nil));
You could just chain as many arguments as you like. The latter had to be null terminated.
source to share