Positron Compiler Documentation

Procedure Return Variable

Positron16 · Revision 1.1.3.5 · PDF pages 58–60

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
View original PDF page 58Open page in PDFOriginal PDF page 58

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 ExitProc ' Perform a test and return early if required
  Result = Result + 1         ' Otherwise… Add one to it
EndProc

A return parameter can also be aliased to an SFR (Special Function Register). For example:

Proc MyProc(), WREG0

As with standard variables, the aliased return parameter can also be cast to a type that has fewer bytes. For example, a Word variable can be cast to a Byte type, or a Dword can be cast to a Word or Byte type etc…

Proc MyProc(), WREG2.Byte0

Below is an example procedure that mimics the compiler’s 16-bit Dig command.

  Device = 24EP128MC202           ' Select the device to compile for
  Declare Xtal = 140.03 ' Tell the compiler the device will be running at 140.03MHz
  Declare Hserial_Baud = 9600    ' UART1 Baud rate
  Declare Hrsout1_Pin = PORTB.11  ' 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 Word, 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:
' Setup the Oscillator to operate the device at 140.03MHz
' Fosc = (7.37 * 76) / (2 * 2) = 140.03MHz
  PLL_Setup(76, 2, 2, $0300)
  RPOR4.Byte1 = 1                         ' Make PPS Pin RB11 U1TX

  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)
View original PDF page 59Open page in PDFOriginal PDF page 59
'--------------------------------------------------------------------------
' Configure for internal 7.37MHz oscillator with PLL
' OSC pins are general purpose I/O
'
    Config FGS = GWRP_OFF, GCP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_ON, PWMLOCK_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, PLLKEN_ON, FWDTEN_OFF
    Config FPOR = ALTI2C1_ON, ALTI2C2_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF
View original PDF page 60Open page in PDFOriginal PDF page 60