Does any dialect of Pascal allow a variable number of arguments?

会有一股神秘感。 提交于 2019-12-13 16:31:19

问题


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 ?


回答1:


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




回答2:


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]);



回答3:


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+)




回答4:


You are probably thinking of a library that was available for Turbo Pascal where they had a hack like this. My syntax is a bit a rusty for objects and descending 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 wanted. The last one had to be terminated with nil.



来源:https://stackoverflow.com/questions/1459775/does-any-dialect-of-pascal-allow-a-variable-number-of-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!