Positron Compiler Documentation

Sub-EndSub

Positron8 · Revision 4.0.6.5 · PDF page 461

Sub Label Name() BASIC commands inside the Sub EndSub

Create a subroutine with a start directive (Sub) and an end directive (EndSub).

Label Name is the name of the subroutine.

' Create a subroutine to flash an LED 10 times
  Device = 16F1829             ' Select the device to compile for
  Declare Xtal = 20

  Dim FlashAmount as Byte      ' Create a variable for the amount of LED flashes
  Symbol LED = PORTB.0        ' Create a name for the LED’s Port and Pin

  Do                           ' Create a loop
     FlashLED()               ' Call the subroutine
     DelayMs 1000             ' Delay for 1 second
  Loop                       ' Loop forever

' Create a subroutine that will flash an LED
'
  Sub FlashLED()
     For FlashAmount = 1 to 10 ' A loop for the amount of flashes
       High LED               ' Illuminate the LED
       DelayMs 500             ' Wait for half a second
       Low LED                 ' Extinguish the LED
       DelayMs 500             ' Wait for half a second
     Next                      ' Close the loop
  EndSub                      ' End the subroutine and return from it

The EndSub directive will produce a Return command and exit the subroutine as normal. There is also an ExitSub command that will create a Return command and return from the subroutine.

' Create a subroutine that will flash an LED and exit when required
'
  Sub FlashLED()
     For FlashAmount = 1 to 100 ' A loop for the amount of flashes
       High LED               ' Illuminate the LED
       DelayMs 500             ' Wait for half a second
       Low LED                 ' Extinguish the LED
       DelayMs 500             ' Wait for half a second
       If FlashAmount >= 10 Then ExitSub  ' Exit the subroutine after 10 flashes
     Next                      ' Close the loop
  EndSub                      ' End the subroutine and return from it

Calling a sub only requires the name, and a pair of parenthesis after it. This makes a program easier to read:

MySub()   ' Call the subroutine named MySub

See also : GoSub, Proc...EndProc.

View original PDF page 461Open page in PDFOriginal PDF page 461