Return
Syntax
Return
or
Return Variable
Availability
All devices. But a parameter return from a standard subroutine is only supported with 18F devices. For better results, use Proc-EndProc.
Overview
Return from a subroutine.
If using an 18F device, a parameter can be pushed onto a software stack before the return mnemonic is implemented.
Variable is a user defined variable of type Bit, Byte, Word, Long, Dword, Float, Array, String, or Constant value, that will be pushed onto the stack before the subroutine is exited.
Example
' Call a subroutine with parameters
'
Device = 18F26K40
' Compiler for an 18F device
Declare Xtal = 20
' Tell the compiler the device is operating at 20MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Declare Stack_Size = 20 ' Create a small stack capable of holding 20 bytes
Dim Wrd1 as Word
' Create a Word variable
Dim Wrd2 as Word
' Create another Word variable
Dim Receipt as Word
' Create a variable to hold result
Wrd1 = 1234
' Load the Word variable with a value
Wrd2 = 567
' Load the other Word variable with a value
'
' Call the subroutine and return a value
'
GoSub AddThem [Wrd1, Wrd2], Receipt
HRsoutLn Dec Receipt
' Display the result as decimal
Stop
'
' Subroutine starts here. Add two parameters passed and return the result
'
AddThem:
Dim AddWrd1 as Word
' Create two uniquely named variables
Dim AddWrd2 as Word
Pop AddWrd2
' Pop the last variable pushed
Pop AddWrd1
' Pop the first variable pushed
AddWrd1 = AddWrd1 + AddWrd2
' Add the values together
Return AddWrd1
' Return the result of the addition
In reality, what's happening with the Return in the above program is simple, if we break it into its constituent events: -
Push AddWrd1
Return
Notes
The same rules apply for the variable returned as they do for Pop, which is after all, what is happening when a variable is returned.
Return resumes execution at the statement following the GoSub which called the subroutine.
Using stack parameters with Gosub and Return are no longer recommended or required because true procedures are now implemented with the Positron8 compiler.
Note.
The Return with a parameter from the stack is now legacy because the Positron8 compiler now has true procedures that can return a value a lot more efficiently.