Passing arguments to write within a procedure

74 views Asked by At

How can I pass arguments from my procedure to a call of write called inside ?

Something quite like that:

procedure smth (args: alltypes);
begin
    write(args);
end;
2

There are 2 answers

0
Abelisto On BEST ANSWER

If you want to use your function with any number/type of argument in Write manner, like smth(3, 'aaa', 5.6) - it is impossible as i know. However you can use array of ... type for argument to pass to the procedure any number of arguments.

Here is an example:

program wrt;

{$mode objfpc}{$H+}

uses
    sysutils, variants;

procedure test1(args: array of Variant);
var
    i: Integer;
begin
    for i := Low(args) to High(args) do
        Write(args[i]);
    Writeln;
end;

procedure test2(fmt: string; args: array of const);
begin
    Writeln(Format(fmt, args));
end;

begin
    test1([1, 'aaa', 3.5, False]);
    test2('%d %s %g, %s', [1, 'aaa', 3.5, BoolToStr(False, True)]);
end.
0
Kryštof Řeháček On

For example:

procedure write( text : string );
begin
    write( text );
end;

But if you want to override your function. You have to read that topic HERE. That will allow you to make function with more type of arguments.