ooPIC Logo

Procedure Declaration

Main Index
Language Idx
Back to top of pageDescription:
 A group of statements that specifies the beginning and end of section of code that forms the body of a named procedure. Procedures can be called by including the name of the Procedure in code upon which the Program Flow will branch to, execute the code in, and then return from the Procedure.

When the ooPIC is initially powered on, or has just encountered a reset, the Program Flow will start execution with the Main Procedure named "main".

Back to top of pageSyntax:
 The following shows the format of the Procedure Construct.
Visual Basic & BASICC & Java
Sub <procedurename>(<arglist>)
  <statements>
  [Exit Sub]
  <statements>
End Sub
sub void <procedurename>(<arglist>){
  <statements>
  [return;]
  <statements>
}

The following table lists the elements of the Procedure Construct syntax.

ElementDescription
BasicC & Java
SubSubProcedure Construct beginning keyword. C and Java still need the sub, this is nonstandard C nor is it Java.
<procedurename><procedurename>The name of the Procedure to branch to. Follows standard naming conventions.
<arglist><arglist>A list of Values or Variables that are used for passing values to the Procedure.  The format of the argument list is shown below.
NOTE: This element is only available in ooPIC Compiler Version 3.0 and greater. 
<statements><statements>One or more statements executed when the Procedure is called.
Exit SubbreakProcedure Termination keyword. Can occur any number of times within the <statements>.
End Sub Procedure Construct ending Keyword


The following shows the format of the <arglist> element.

Visual Basic & BASIC
([<id1> As <type1>[, <idX> As <typeX>[,...]]])
C & Java
([<type1> <id1>[, <typeX> <idx>[,...]]][void])

The following table lists the elements of the <arglist> format.

ElementDescription
BasicC & Java
<id1><id1>The name of the first optional identifier. Follows standard naming conventions.
NOTE: In C & Java syntax, if no identifiers are defined, the word "void" must be present.
<type1><type1>If versions of the compiler Pryor to verstion6, the type of variable to create an instance of. May be one of the following; Byte, Word. In compiler versions prior to 3.05 the argument list used Objects: oBit, oByte, oNibble, oWord.
<Idx><idx>The name of subsequent optional identifiers. Follows standard naming conventions.
<typeX><typeX>If versions of the compiler Pryor to verstion6, the type of variable to create an instance of. May be one of the following; Byte, Word. In compiler versions prior to 3.05 the argument list used Objects.
The Objects that could be used in pre-3.05 versions were one of the following; oBit, oByte, oNibble, oWord.
Back to top of pageOperation:
 

Basic Syntax:
When a Procedure Call is encountered during the program's execution, any values specified in the <arglist> element are copied to the values used by the Procedure specified by the <procedurename> element, the current program flow position is saved and the program flow branches to the Procedure  

After branching to the Procedure, the program flow executes the statements within the Procedure until either the end of the Procedure or the Exit Sub keywords are encountered, at which time the original program flow position is retrieved and the program flow execution resumes at its original position.

C Syntax:
Note: In C, Procedures are called Functions, even though they do not return a value.
When a Function's identifier, specified by the <procedurename> element, is encountered during the program's execution, any values specified by the <arglist> element are copied to the values used by the Function, the current program flow position is saved and the program flow branches to the specified Function.

After branching to the Function, the program flow executes the statements within the Function until the end of the Function is encountered, at which time the original program flow position is retrieved and the program execution resumes at its original position.

Java Syntax:
Note: In Java, Procedures are called methods due to their encapsulation within a Class.
When a Method's identifier, specified by the <procedurename> element, is encountered during the program's execution, any values specified by the <arglist> element are copied to the values used by the Method, the current program flow position is and the program flow branches to the specified Method.

After branching to the Method, the program flow executes the statements within the Method until the end of the Method is encountered, at which time the original program flow position is retrieved and the program executions resumes at its original position.

Back to top of pageRemarks:
 
All executable code must be within the Main Procedures, Sub Procedures or in Functions

You cannot define a Procedure or a Function inside of a Procedure.

At the minimum, A Main Procedure named "main" is required for any ooPIC program.

Procedures can be recursive.  See caution below.

Any  values passed to any Procedure are passed by value. 

The ability to pass values to Procedures was added to the ooPIC compiler in Version 3.00.

In C and Java syntax, the use of prototypes is not necessary.  In their place, the Sub keyword is used to announce the Procedure.

As of version 3.05 of the compiler, the argument list was changed to use variables which in version B.1.0 of the firmware will use the second bank of memory.

Caution: Procedures can be recursive; that is, they can call themselves. This can lead to stack overflow and must be used carefully.

Back to top of pageExample:
 In the following example, a program is shown that branches to, and returns from, a procedure named "Blink" two times.
Visual BasicC & Java
Dim A As New oDIO1

Sub Main()
  A.IOLine = 31
  A.Direction = cvOutput
  Call Blink
  Call Blink
End Sub

Sub Blink() 
  A.Value = cvOn
  ooPIC.Delay = 100
  A.Value = cvOff
  ooPIC.Delay = 100
End Sub
oDIO1 A = New oDIO1;

Sub Void main(Void){
  A.IOLine = 31;
  A.Direction = cvOutput;
  Blink;
  Blink;
}

Sub Void Blink(Void){ 
  A.Value = cvOn;
  ooPIC.Delay = 100;
  A.Value = cvOff;
  ooPIC.Delay = 100;
}
BASIC 
A As oDIO1(31,cvOutput)
Call Blink
Call Blink

Sub Blink() 
  A.Value = cvOn
  ooPIC.Delay = 100
  A.Value = cvOff
  ooPIC.Delay = 100
End Sub
 

In the following example, a program is shown that branches to, and returns from, a procedure named "Blink" once, but passes an argument of 5, which is used by the procedure to determine how many times to blink.
Visual BasicC & Java
Dim A As New oDIO1

Sub Main()
  A.IOLine = 31
  A.Direction = cvOutput
  Call Blink(5)
End Sub

Sub Blink(xTimes As Byte)
  While xTimes.Value > 0
    A.Value = cvOn
    ooPIC.Delay = 100
    A.Value = cvOff
    ooPIC.Delay = 100
    xTimes = xTimes - 1
  Wend
End Sub
oDIO1 A = New oDIO1;

Sub Void main(Void){
  A.IOLine = 31;
  A.Direction = cvOutput;
  Blink(5);
}

Sub Void Blink(Byte xTimes){ 
  While (xTimes.Value > 0){
    A.Value = cvOn;
    ooPIC.Delay = 100;
    A.Value = cvOff;
    ooPIC.Delay = 100;
    xTimes = xTimes - 1;
  }
}
BASIC 
Dim A As New oDIO1(31,cvOutput)
Call Blink(5)

Sub Blink(xTimes As Byte)
  While xTimes.Value > 0
    A.Value = cvOn
    ooPIC.Delay = 100
    A.Value = cvOff
    ooPIC.Delay = 100
    xTimes = xTimes - 1
  Wend
End Sub
 

The following example shows a Procedure Call that passes two arguments to a procedure named "Fred". The first argument is a constant of 5, and the second argument is the value of A.
Visual Basic & BASICC & Java
Call Fred(5, A.Value)
  Blink(5, A.Value);

Back to top of pageRelated Items:

 Procedure Call
Back to top of pageVersion History and Bug List:
 Firmware Ver A1: Introduced.

Bugs: No known bugs.


ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.