 |
Procedure Declaration
|
|
|
Description: |
| | 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". |
Syntax: |
| | The following shows the format of the Procedure Construct.
| Visual Basic & BASIC | C & 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.
| Element | Description |
| Basic | C & Java |
| Sub | Sub | Procedure 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 Sub | break | Procedure 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.
| Element | Description |
| Basic | C & 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. |
|
Operation: |
| | 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. |
Remarks: |
| |
| 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. |
|
Example: |
| | In the following example, a program is shown that branches to, and returns from, a procedure named "Blink" two times.
| Visual Basic | C & 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 Basic | C & 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 & BASIC | C & Java |
Call Fred(5, A.Value) | Blink(5, A.Value); |
|
Related Items:
|
| | Procedure Call |
Version History and Bug List: |
| | Firmware Ver A1: Introduced. Bugs: No known bugs. |
|