views:

151

answers:

3

This is a question for the older programmers.

Years ago, I encountered a dialect of Pascal which allowed a variable number of arguments, through some kind of extension.

Does anyone know of a current dialect of Pascal which allows a variable number of arguments?

Given that Pascal is not as popular as it used to be, I wouldn't be surprised if the answer is no.

BTW, it is more correct, isn't it, to say variable number of arguments, rather than parameters ?

A: 

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 parameters:

procedure Message(const AMessage: string; const AArgs: array of const);

Message('Hello %s', [Name]);
Message('%s %s', [Greeting, Name]);
Gamecat
Great, I just qualified myself as old ;-).
Gamecat
It looks like you're explicitly constructing the array in your second example, correct?
kdgregory
@Gamecat, I'll put it down to your 30 years experience, although there's nothing wrong with *age*. Thanks.
pavium
+3  A: 

No. The answer is based on the Pascal dialects that I have used; others may be different.

The reason is that Pascal pushes arguments onto the stack frame in order, so all arguments are accessed via a fixed offset from the stack pointer. C, by comparison, pushes arguments in reverse order, so defined parameters are at fixed offset, and you can access "extra" arguments via 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 parameter versus argument: as I learned it, the function (method) defines its parameters, the caller passes arguments. That definition came, I believe, from a Fortran manual, so that should give you an idea of how old I am :-)

kdgregory
Thanks, I'll see if I can find some description of the internals of Apollo DOMAIN Pascal, the dialect which allowed variable lists of arguments (and which I'm *still* using). Your explanation has the ring of truth about it.
pavium
If you're still using it, you should be able to disassemble an example with different calling sequences. I wouldn't be surprised if they use the C calling conventions.
kdgregory
A: 

GNU-Pascal (gcc based) afaik maps 1:1 to C support. using function something(arg:pchar;...) like syntax

Delphi/Free Pascal has "array of const" support, which is a typesafe version, and a varargs directive for the C interfacing (D6 or D7+)

Marco van de Voort