Search code examples
argumentspascalprocedurelazarusfpc

Passing arguments to write within a procedure


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;

Solution

  • 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.