Returning multiple values from a function

Vino.B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 4 02:22:25 PDT 2017


On Monday, 4 September 2017 at 07:40:23 UTC, crimaniak wrote:
> On Monday, 4 September 2017 at 07:27:12 UTC, Vino.B wrote:
>> Hi,
>>
>>  Can you help me in how to return multiple values from a 
>> function, the below code is throwing an error as below
>
> import std.stdio: writeln;
> import std.typecons: tuple, Tuple;
>
> Tuple!(int, string[]) Params () {
>     return tuple(1, ["C:\\Temp\\TEAM1\\BACKUP", 
> "C:\\Temp\\TEAM2\\ARCHIVE"]);
> }
>
> void main (){
>     Params.writeln;
> }

Hi,

  Thank you very much, i have used your idea and was able to 
resolve, and i need one more favor. the below code outputs the 
value but i need the name of the variable + value as below.

Output :
1
2
["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Required Output:
Test1 = 1
Test2 = 2
Path = ["C:\\Temp\\TEAM1\\BACKUP", "C:\\Temp\\TEAM2\\ARCHIVE"]

Program:
import std.stdio: writeln;
import std.typecons: tuple, Tuple;
import std.array: appender;

Tuple!(int,int, string[]) Params () {
int Test1;
int Test2;
string[] File1;
string[] File2;
auto Path = appender!(string[]);
Test1 = 1;
Test2 = 2;
File1 = ["C:\\Temp\\TEAM1\\BACKUP"];
File2 = ["C:\\Temp\\TEAM2\\ARCHIVE"];
Path ~= File1;
Path ~= File2;
return tuple (Test1, Test2, Path.data);

}

void main (){
writeln(Params[0]);
writeln(Params[1]);
writeln(Params[2]);
}
return tuple (Test1, Test1Test2, Path.data);

}

void main (){
writeln(Params[0]);
writeln(Params[1]);
}

From,
Vino.B


More information about the Digitalmars-d-learn mailing list