Passing values to and from a function
This article highlights the differences between passing by value and passing by reference, and gives examples of how this is used in Functions.
Passing variables
Normally, when creating and using a function in EasyLanguage, a copy of a variable gets send to the function, which in turn performs calculations with it, and the result is returned. This ensures that the original variable would not be changed by the calculations that the function performs. This process is called passing by value.
Another way to pass variables to a function is by reference. In that case the actual variable gets send to the function, where it would undergo a calculation. After that the variable, with a newly assigned value, is send back to where the function was called.
Real-life example of 'passing'
This is a somewhat abstract concept, but let’s assume I’ve got a handwritten piece of paper and a friend wants to borrow it. If I make a copy of this piece of paper, and give the copy to my friend, I’m passing by value and anything that he writes on the paper will not affect my original version (since I gave him a copy). However, if I give the original version to my friend, I’m passing by reference, and any changes he make will change my original paper.
To summarize
- If a variable is passed by value, a copy of the variable is send leaving the original variable value intact.
- If a variable is passed by reference, the actual variable is send and any calculations will result in a changed value of this variable.
One of the biggest benefits of using passing by reference in EasyLanguage is that it’s possible to send multiple variables to a function and perform calculations on them, instead of having to use multiple function for each of these variables.
Example of passing by value
The example below uses passing by value through the NumericSimple as input. The function takes three variables as input, performs calculations with these values, and returns one variable: the profit of a forex trade converted to the base currency of the trader.
// Example function 'ForexPositionProfit': Calculating Forex position profits with a 'passing by value' function
Inputs:
FXPositionProfit(NumericSimple), FXCommission(NumericSimple), FXSlippage(NumericSimple);
Variables:
ExchangeRate(1.40), ProfitInBaseCurrency(0), CommissionInBaseCurrency(0), SlippageInBaseCurrency(0);
// Calculate the forex profits to the base currency with the fixed exchange rate
ProfitInBaseCurrency = FXPositionProfit / ExchangeRate;
CommissionInBaseCurrency = (FXCommission * 2) / ExchangeRate;
SlippageInBaseCurrency = (FXSlippage * 2) / ExchangeRate;
ForexPositionProfit = ProfitInBaseCurrency – (CommissionInBaseCurrency + SlippageInBaseCurrency);
This function would be called in strategies, using the strategy performance reserved words, by:
Variables: ProfitOfTrade(0);
ProfitOfTrade = ForexPositionProfit(PositionProfit(1), Commission, Slippage);
In this example, a copy of the three variables (the position profit, commission, and slippage) gets send to the function, which uses these in the calculation of the profit of the trade.
Passing by reference in EasyLanguage
If someone also wants to calculate the commission and slippage into the base currency of the account, one can create extra functions for this that will return those values. However, it's also possible to create one function which uses passing by reference to return these three values.
Passing variables by reference has the benefit of being able to calculate multiple variables in a function, without having to create a different function for each variable you’d like to calculate. For example, the code above can be slightly changed to calculate more variables in the function without much extra code – and thus being a more quicker and efficient way of calculating these variables than creating a custom function for each of them.
Instead of using the NumericSimple as input for the function, which sends a copy of the variable to the function, we’ll use the NumericRef, which passes our numeric variable by reference to the function.
// Example function 'ForexPositionProfit': Calculating Forex position profits with a 'passing by reference' function
Inputs:
FXPositionProfit(NumericRef), FXCommission(NumericRef), FXSlippage(NumericRef);
Variables:
ExchangeRate(1.40);
// Calculate the forex profits to the base currency with the fixed exchange rate
FXPositionProfit = PositionProfit(1) / ExchangeRate;
FXCommission = Commission / ExchangeRate;
FXSlippage = (Slippage * 2) / ExchangeRate;
ForexPositionProfit = 1; // dummy variable
In the example above, instead of sending a copy of the variables to the function, the function uses the actual values, assigns a new value to them, and returns these variables (with newly calculated values) to the signal that called the function. Since each function in EasyLanguage needs to return a value, the example function ForexPositionProfit above returns a 1 as a dummy variable.
This passing by reference function will get called by:
Variables: ProfitInEuro(0), CommissionInEuro(0), SlippageInEuro(0);
// Setting the variables with the strategy performance data
ProfitInEuro = PositionProfit(1);
CommissionInEuro = Commission;
SlippageInEuro = Slippage;
// Calling the function that will convert the variables using the fixed exchange rate
value1 = ForexPositionProfit(ProfitInEuro, CommissionInEuro, SlippageInEuro);
// The variables, send by reference to the function,
// will now have the value that was assigned to them by the function
Though the example above uses NumericRef for passing by reference, the following types of variables can be passed by reference in EasyLanguage: