# Function Variable Data Survives Between Calls

### Function Variable Data Survives from One Call to the Next – A Pretty Nifty Tool in EasyLanguage!

Creating a function that can store data and then have that data survive on successive function calls without having to pass information back and forth is really a cool and powerful tool in EasyLanguage.  In most programming languages, the variables defined in a function are local to that particular bit of code and once program execution exits the function, then the data is destroyed.  There are two exceptions (in other languages) that come to mind – if the variable is passed back and forth via their addresses, then the data can be maintained or if the variable is global in scope to the function and the calling program.  EasyLanguage prevents you from having to do this and this can definitely save on headaches.  I wrote a function that defines an array that will hold a list of trades.  Once the number of trades reaches a certain level, I then calculate a moving average of the last 10 trades.  The average is then passed back to the calling strategy.  Here is the simple code to the function.

``{Function Name:   StoreTradesFunc by George Pruitt}{Function to Calculate the average trade for past N trades. ---------------------------------------------------------- Function remembers the current trade count in tradeCnt. It also remembers the values in the array tradesArray. It does this between function calls.  Values - simple and array - undoubtedly are global to the function} input: avgTradeCalcLen(numericSimple);vars: totalTradesCount(0),tradeCnt(0);array: tradesArray[500](0);totalTradesCount = totalTrades;If totalTradesCount > totalTradesCount[1] thenbegin	tradeCnt = tradeCnt + 1;	tradesArray[tradeCnt] = positionProfit(1);//	print("Storing data ",tradesArray[tradeCnt]," ",tradeCnt);end;If totalTrades > avgTradeCalcLen thenbegin	Value2 = 0;	For value1 = totalTrades downTo totalTrades - avgTradeCalcLen	begin		Value2 = value2 + tradesArray[value1];	end;	print("Sum of last 10 Trades: ",value2);	StoreTradesFunc = value2/avgTradeCalcLen;end;``
Store A List of Trades in a Function

### Discover more from George Pruitt

Subscribe to get the latest posts sent to your email.