Pascal-如何将可变数量的参数传递给子程序? (可变函数) [英] Pascal - How to pass variable number of parameters to a subprogram ? (variadic function)

查看:123
本文介绍了Pascal-如何将可变数量的参数传递给子程序? (可变函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近不得不面对这个问题,即如何在Pascal中将1、2、3、9、38919、0或任意随机数的参数传递给函数或过程?我想制作一个子程序,该子程序接受与我想传递的参数一样多的参数,例如writeln.

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.');


我在网上搜索了一些指南或其他内容,但是我发现的唯一相关主题是这些 一个,这有助于我更多地了解自己的问题,但我仍然没有不知道如何在Pascal上执行此操作.
我还发现了,但是我不确定我是否真的了解它说什么. (而且我也不知道适用于Free Pascal的内容是否也适用于其他编译器)

有任何想法吗 ? :/


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 ? :/

推荐答案

首先,writeln是一种语言构造,而不是一个函数.您不能为自己的功能模仿它. (不过,您可以重新路由writeln输出,并且FPC具有 writestr 可以写成字符串)

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)

常量数组的语法更多面向Delphi. 打开数组也是面向Delphi的,但是Turbo Pascal有自己的形式.不过,它仅适用于一种类型.

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.

但是,由于经典的pascal无法处理可变参数,因此,如果要这样做,就无法避免使用扩展名.

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

这篇关于Pascal-如何将可变数量的参数传递给子程序? (可变函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆