简体   繁体   中英

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;

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM