Wed Aug 12, 2020 1:30 am

## Returning multiple values from a function

Share ideas, code, best practices and techniques with other community members
I just learned something today that is really neat and elegant. If you need to return multiple values from a function, simply wrap the values in a MODULE and make each value an exported attribute, then return the module.

Here is an example, which is the code from Std.Date.ToGregorianYMD() in the 5.0.0 version of the standard library:

Code: Select all
`EXPORT ToGregorianYMD(Days_t days) := FUNCTION    //See Fliegel and van Flandern (1968) and other quoted sources (e.g., http://www.ortelius.de/kalender/calc_en.php)    //Process as 4, 100 and 400 year cycles.    daysIn4Years := 3*365+366;    daysIn100Years := 25*daysIn4Years-1;    daysIn400Years := 4*daysIn100Years+1;    //Calulate days in each of the cycles.    adjustedDays := days - GregorianDateOrigin;    num400Years := adjustedDays div daysIn400Years;    rem400Years := adjustedDays % daysIn400Years;    num100Years := ((rem400Years div daysIn100Years + 1) * 3) DIV 4;    rem100Years := rem400Years - num100Years * daysIn100Years;    num4Years := rem100Years div daysIn4Years;    rem4Years := rem100Years % daysIn4Years;    years := ((rem4Years div 365 + 1) * 3) DIV 4;    numdays := rem4Years - years * 365;    //Now calculate the actual year, month day    y := num400Years * 400 + num100Years * 100 + num4Years * 4 + years;    m := (numdays * 5 + 308) div 153 - 2;    d := numdays - (m + 4) * 153 div 5 + 122;    result := MODULE        EXPORT year := (y + (m + 2) div 12) - YearDelta;        EXPORT month := (m + 2) % 12 + 1;        EXPORT day := d + 1;    END;    return result;END;`

The example from the documentation illustrates how you use this:

Code: Select all
`IMPORT STD;INTEGER2  MyYear  := 2012;UNSIGNED1 MyMonth := 1;UNSIGNED1 MyDay   := 1;J := STD.Date.FromGregorianYMD(MyYear,MyMonth,MyDay);    //J contains 734503Y := STD.Date.ToGregorianYMD(J).Year;   //Y contains 2012M := STD.Date.ToGregorianYMD(J).Month;  //M contains 1D := STD.Date.ToGregorianYMD(J).Day;    //D contains 1`

Pretty neat.

Cheers,

Dan
DSC

Posts: 568
Joined: Tue Oct 18, 2011 4:45 pm

Dan,

Thanks for bringing this up. It's one of those "little" things in ECL that can easily get overlooked, but can be quite useful.

Richard
rtaylor

Posts: 1552
Joined: Wed Oct 26, 2011 7:40 pm

Hi Richard / All,

I've found this:
Code: Select all
`RETURN MODULE    EXPORT Results := h.SomeData;    EXPORT Report  := h.ReportOnSaidData;END;`

particularly useful.
Where I'm returning results, but also have to tie a report to those same results.

Yours
Allan
Allan

Posts: 429
Joined: Sat Oct 01, 2011 7:26 pm

This post is useful to me as I was also returning multiple values from a function but was not able to do it.
sarahah

Posts: 1
Joined: Wed Jan 16, 2019 5:36 am

Thanks for this valuable tip. It will be great help for my project.
yugdewalkar

Posts: 1
Joined: Tue Mar 05, 2019 7:20 am

### Who is online

Users browsing this forum: No registered users and 0 guests