User Tools

Site Tools


returning_20an_20array_20from_20a_20function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

returning_20an_20array_20from_20a_20function [2018/03/31 13:19]
127.0.0.1 external edit
returning_20an_20array_20from_20a_20function [2018/04/17 18:32] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, November 2012//\\ \\  Modifying the //​contents//​ of an array (i.e. its //​elements//​) within a PROC or FN is straightforward,​ because arrays are passed **by reference** and therefore any modifications to their contents remain intact after the FN or PROC returns.\\ \\  However this necessarily means that the array must first be created (DIMensioned) **outside** the FN or PROC, and this may not always be convenient or desirable. The alternative is to create the array **inside** the function and then to return it to the caller. The conventional way of doing that is to use the RETURN qualifier; for example this procedure creates an //identity matrix// (having ones on the leading diagonal and zeroes elsewhere): \\  //by Richard Russell, November 2012//\\ \\  Modifying the //​contents//​ of an array (i.e. its //​elements//​) within a PROC or FN is straightforward,​ because arrays are passed **by reference** and therefore any modifications to their contents remain intact after the FN or PROC returns.\\ \\  However this necessarily means that the array must first be created (DIMensioned) **outside** the FN or PROC, and this may not always be convenient or desirable. The alternative is to create the array **inside** the function and then to return it to the caller. The conventional way of doing that is to use the RETURN qualifier; for example this procedure creates an //identity matrix// (having ones on the leading diagonal and zeroes elsewhere): \\ 
 +<code bb4w>
         DEF PROCidn(RETURN i(), n%)         DEF PROCidn(RETURN i(), n%)
         LOCAL i%         LOCAL i%
Line 9: Line 10:
         NEXT         NEXT
         ENDPROC         ENDPROC
 +</​code>​
 The procedure is called with two parameters: the array to be created (which must not have been previously dimensioned) and the required size (maximum row and column index), for example:​\\ ​ The procedure is called with two parameters: the array to be created (which must not have been previously dimensioned) and the required size (maximum row and column index), for example:​\\ ​
 +<code bb4w>
         PROCidn(identity(),​ 3)         PROCidn(identity(),​ 3)
 +</​code>​
 This works well, but in one respect it is not particularly '​satisfying'​. Normally, when you want to return a value (numeric or string) from a sub-module you would use a **function** (FN) rather than a **procedure** (PROC). A function provides a more natural means to return a value, since it may be assigned directly to a variable:​\\ ​ This works well, but in one respect it is not particularly '​satisfying'​. Normally, when you want to return a value (numeric or string) from a sub-module you would use a **function** (FN) rather than a **procedure** (PROC). A function provides a more natural means to return a value, since it may be assigned directly to a variable:​\\ ​
 +<code bb4w>
         result = FNfunction(parameters)         result = FNfunction(parameters)
 +</​code>​
 Wouldn'​t it be nice if you could do the same with an array, in other words create an array within a function and return it to the caller in the same way as a regular function returns a number or a string? In fact, with a little bit of trickery, you **can**!\\ \\  Using the example of a function to create an identity matrix, this is how you would code it:\\  Wouldn'​t it be nice if you could do the same with an array, in other words create an array within a function and return it to the caller in the same way as a regular function returns a number or a string? In fact, with a little bit of trickery, you **can**!\\ \\  Using the example of a function to create an identity matrix, this is how you would code it:\\ 
 +<code bb4w>
         DEF FNidn(n%)         DEF FNidn(n%)
         LOCAL i%, !^i()         LOCAL i%, !^i()
Line 21: Line 28:
         NEXT         NEXT
         = !^i()         = !^i()
 +</​code>​
 Note particularly the use of **!^** in both the LOCAL statement and when the array is returned from the function. The function can then be called as follows:​\\ ​ Note particularly the use of **!^** in both the LOCAL statement and when the array is returned from the function. The function can then be called as follows:​\\ ​
 +<code bb4w>
         !^identity() = FNidn(3)         !^identity() = FNidn(3)
 +</​code>​
 Again, note the use of an //array pointer// using **!^**.\\ \\  This may be a useful technique when translating from a language that can directly return an array from a function, or simply to create a more elegant code solution. Again, note the use of an //array pointer// using **!^**.\\ \\  This may be a useful technique when translating from a language that can directly return an array from a function, or simply to create a more elegant code solution.
returning_20an_20array_20from_20a_20function.txt ยท Last modified: 2018/04/17 18:32 by tbest3112