Pascal - How to pass variable number of parameters to a subprogram ? (variadic function)

折月煮酒 提交于 2019-12-02 05:40:25

问题


I recently had to face this problem, which is, how can I pass 1, 2, 3, 9, 38919, 0 or any random number of arguments to a function or a procedure in Pascal ? I want to make a subprogram that accepts as many parameters as I want to pass it, like writeln.

 writeln('Hello, ', name, '.');
 writeln('You were born on ', birthDate, ', and you are ', age, ' years old.');


I searched the web for some guide or whatever, but the only related threads I found were these ones, which helped me understanding my problem a bit more, but I still don't know how to do this in Pascal.
I also found this, but I'm not sure I really understood what it says. (and I don't know either if what applies to Free Pascal applies to other compilers too)

Any ideas ? :/


回答1:


First and for all, writeln is a language construct, and not a function. You can't imitate it for your own functions. (you can reroute writeln output though, and FPC has writestr that can writeln to string)

The array of const syntax is more Delphi oriented. Open array is Delphi oriented too, but Turbo Pascal had a own form. It is only for one type though.

But since classic pascal has no way of doing variadic parameters, if you want this, you can't avoid using extensions.



来源:https://stackoverflow.com/questions/21654438/pascal-how-to-pass-variable-number-of-parameters-to-a-subprogram-variadic-f

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