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

查看:23
本文介绍了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.');


我在网上搜索了一些指南或其他内容,但我发现的唯一相关主题是这些 ones,这帮助我更了解我的问题,但我仍然没有'不知道如何在 Pascal 中做到这一点.
我还找到了 this,但我不确定我是否真的理解它说什么.(我也不知道适用于 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)

数组 const 语法更多面向德尔福.Open array 也是面向 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天全站免登陆