Ref function not working on arguments passed by reference

I am passing arguments by reference so that the function can return multiple values. However in the function I use the "ref" function on these variables so that I can detect a change. This does not seem to work. (I can get around this by loop processing though.)

It does seem that other functions do not work on these variables.

Is this a known limitation of passing arguments by reference or am I doing something wrong?`

	function TrendFunction( Array, Lookback, TrendMode, TrendModeChange)
	{
		Trend 			= MA( Array, Lookback );
		TrendMode 		= IIf( Trend > Ref(Trend, -1 ), 1, -1 );
		TrendModeChange = iif( TrendMode != Ref(TrendMode, -1 ), 1, 0 );
	
		Temp1 			= Ref( Trend, -1);		
		Temp2			= Ref( TrendMode, -1);
	
		for( bar = 0; bar < BarCount; bar++ )
		{
		if(bar > 10) _TRACE ("#12 bar " + bar + " Trend " + Trend[ bar ] 
		+ " TrendMode " + TrendMode[ bar ] + " TrendModeChange " + TrendModeChange[ bar ]
		+ " Temp1 " + Temp1[ bar ] + " Temp2 " + Temp2[ bar ] 
			);
		}
	
		return Trend; 
	}
	
	Trend 				= 0;
	TrendMode 			= 0;
	TrendModeChange 	= 0;
	Trend 				= TrendFunction( Close, 10, &TrendMode, &TrendModeChange );
	
	Temp3 				= Ref( Trend, -1);		
	Temp4				= Ref( TrendMode, -1);

	for( bar = 0; bar < BarCount; bar++ )
	{
		if(bar > 10) _TRACE ("#32 bar " + bar + " Trend " + Trend[ bar ] 
		+ " TrendMode " + TrendMode[ bar ] + " TrendModeChange " + TrendModeChange[ bar ]
		+ " Temp3 " + Temp3[ bar ] + " Temp4 " + Temp4[ bar ] 
		);
	}
	
	Buy = 1;
	Sell = 0;

It is working perfectly fine. Just not the way you assume.
Built-in (internal) functions accept ONLY by value semantics and NEVER modify passed arguments.

@Tomasz, I wonder if this also explains the results of this sample code I posted some time ago.

The first example works appropriately (using some local variables and assigning them to the ones passed by reference at the end of the function).

In the second case (not working "as I assumed" code), I assigned to the passed by reference variables the return values of "iif" functions, and then I attempted to use them to plot lines (with NO plot output).
But both code samples returned the correct (changed) values to the calling code.

Any comment to further clarify the matter will be appreciated.

Built-in functions do not expect getting references. There should be an error message on such attempt.

1 Like

Passing references to built-in functions and auto-dereferencing is now supported in version 6.28.0

2 Likes