Domanda

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

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top