If you prefer to avoid globals and want to "simulate" multiple return values from a function it is possible to use the technique explained in this KB article:
A function with multiple return values
As said before, a recent beta AmiBroker introduced the "true" feature to pass parameters by reference.
Re this new feature I have a question for @Tomasz.
For testing purposes, I modified the Automatic support and resistance lines passing to the SupResLevels( ) function the 2 last params by reference (..., byRef1, byRef2)
Here is my modified code sample that works properly:
// requirements
Version( 6.27 );
function SupResLevels( bars, colorUp, ColorDn, byRef1, byRef2 )
{
bi = BarIndex();
lvbi = LastValue( bi );
// return HHV value only for bars starting from the bar where HHV level was established
hv = IIf( bi >= lvbi - LastValue( HHVBars( High, bars ) ), LastValue( HHV( High, bars ) ), Null );
// the same approach for LLV
lv = IIf( bi >= lvbi - LastValue( LLVBars( Low, bars ) ), LastValue( LLV( Low, bars ) ), Null );
// plot levels
Plot( hv, "hv", colorUp, styleDashed | styleThick);
Plot( lv, "lv", ColorDn, styleDashed | styleThick );
// Assing calculated local arrays to param passed by ref
byRef1 = hv;
byRef2 = lv;
}
// price plot
Plot( Close, "By Ref: USING Local Vars - Close", colorDefault, styleBar );
// call function with various parameters
SupResLevels( 20, colorBrightGreen, colorRed, &hv1, &lv1 );
SupResLevels( 100, colorBrightGreen, colorRed, &hv2, &lv2 );
// Exploration
Filter = 1;
AddColumn( C, "Close" );
AddColumn( hv1, "HV1" );
AddColumn( lv1, "LV1" );
AddColumn( hv2, "HV2" );
AddColumn( lv2, "LV2" );
SetSortColumns(1); // sort by ticker
But, if I modify the code in the following way - NOT using local variables inside the function but doing the calculations directly to the passed by ref parameters - the main procedure arrays are filled with correct values (as it is possible to verify step by step with the debugger or using the exploration), but there is NO PLOTTING of the res/sup lines inside the function (and no warning or error messages).
// requirements
Version( 6.27 );
function SupResLevels( bars, colorUp, ColorDn, byRef1, byRef2 )
{
bi = BarIndex();
lvbi = LastValue( bi );
// return HHV value only for bars starting from the bar where HHV level was established
byRef1 = IIf( bi >= lvbi - LastValue( HHVBars( High, bars ) ), LastValue( HHV( High, bars ) ), Null );
// the same approach for LLV
byRef2 = IIf( bi >= lvbi - LastValue( LLVBars( Low, bars ) ), LastValue( LLV( Low, bars ) ), Null );
// plot levels
Plot( byRef1, "hv", colorUp, styleDashed | styleThick); // NO PLOT!
Plot( byRef2, "lv", ColorDn, styleDashed | styleThick); // NO PLOT!
}
// price plot
Plot( Close, "By Ref: NO Local Vars - Close", colorDefault, styleBar );
// call function with various parameters
SupResLevels( 20, colorBrightGreen, colorRed, &hv1, &lv1 );
SupResLevels( 100, colorBrightGreen, colorRed, &hv2, &lv2 );
// Exploration
Filter = 1;
AddColumn( C, "Close" );
AddColumn( hv1, "HV1" );
AddColumn( lv1, "LV1" );
AddColumn( hv2, "HV2" );
AddColumn( lv2, "LV2" );
SetSortColumns(1); // sort by ticker
Here is what I see on my screen:

If possible, I would like to understand what is the reason for this different behavior.
(Test was done using AmiBroker 6.27.1 Beta - 64 Bits)