Return Variable as an Alias to an SFR or Global Variable
As with parameters, a procedure can return an alias to a global variable or SFR (Special Function Register). This can be useful in some programs, and especially if the return variable needs to be a microcontroller SFR.
Example.
' A simple demo to show a return from a procedure placed in a global variable
'
Device = 18F26K40
' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600
Dim MyWord as Word
' Create a global Word variable
'
' Create a demo procedure to multiply 2 parameter values and return the result
' in a global variable
'
Proc TestProc(pValue1 as Word, pValue2 as Word), MyWord
Result = pValue1 * pValue2
EndProc
TestProc(2000, 10)
' Call the procedure to multiply the 2 parameters
HRsoutLn Dec MyWord
' Display the value held in MyWord (The procedure's return)
Notes
The compiler’s implementation of procedures are a powerful feature of the language when used appropriately. Procedures are not supported in every instance of the compiler and if one is not supported within a particular command, a syntax error will be produced. In which case, an intermediate variable will need to be created to hold the procedure’s return result:
MyTemp = MyProc()
The compiler does not re-cycle RAM for parameters or local variables yet, but that is being looked into for future updates. However, more and more devices are offering more RAM available, so it is no longer such an issue because the microcontroller, generally, has more RAM than is required anyway. It also makes the underlying Asm code smaller and faster.