Procedure Return Variable
A procedure can return a variable of any type, making it useful for inclusion within expressions. The variable type to return is added to the end of the procedure's template. For example:
Proc MyProc(), SByte
Result = 10
EndProc
All variable types are allowed as return parameters and follow the same syntax rules as Dim. Note that a return name is not required, only a type. For example:
Proc MyProc(), [12] as Byte ' Procedure returns a 12 element byte array
Proc MyProc(), [12] as Word ' Procedure returns a 12 element word array
Proc MyProc(), [12] as Dword ' Procedure returns a 12 element dword array
Proc MyProc(), [12] as Float ' Procedure returns a 12 element float array
Proc MyProc(), String * 12 ' Procedure returns a 12 character string
In order to return a value, the text “Result” is used. Internally, the text Result will be mapped to the procedure’s return variable. For example:
Proc MyProc(pBytein as Byte), Byte
Result = pBytein ' Transfer the parameter directly to the return variable
EndProc
The Result variable is mapped internally to a variable of the type given as the return parameter, therefore it is possible to use it the same as any other local variable, and upon return from the procedure, its value will be passed. For example:
Proc MyProc(pBytein as Byte), Byte
Result = pBytein ' Transfer the parameter to the return variable
Result = Result + 1 ' Add one to it
EndProc
Returning early from a procedure is the same as returning from a subroutine. i.e. using the Return keyword, or the ExitProc command may be used, which performs a return as well.
Proc MyProc(pBytein as Byte), Byte
Result = pBytein ' Transfer the parameter to the return variable
If pBytein = 0 Then Return ' Perform a test and return early if required
Result = Result + 1 ' Otherwise… Add one to it
EndProc
Below is an example procedure that mimics the compiler’s 16-bit Dig command.
Device = 18F25K22 ' Select the device to compile for
Declare Xtal = 16
Declare Hserial_Baud = 9600 ' UART1 baud rate
Declare HRsout1_Pin = PORTC.6 ' Select pin to be used for USART1 TX
Dim MyWord As Word = 12345
'--------------------------------------------------
' Emulate the 16-bit Dig command's operation
' Input : pWordin holds the value to extract from
' : pDigit holds which digit to extract (1 To 5)
' Output : Result holds the extracted value
' Notes : None
'
Proc DoDig16(pWordin As SWord, pDigit As Byte), Byte
Dim DigitLoop As Byte
pWordin = Abs(pWordin)
If pDigit > 0 Then
For DigitLoop = (pDigit - 1) To 0 Step -1
pWordin = pWordin / 10
Next
EndIf
Result = pWordin // 10
EndProc
'--------------------------------------------------
Main:
HRsout Dec DoDig16(MyWord, 0)
HRsout Dec DoDig16(MyWord, 1)
HRsout Dec DoDig16(MyWord, 2)
HRsout Dec DoDig16(MyWord, 3)
HRsoutLn Dec DoDig16(MyWord, 4)

