 |
Function Declaration
|
|
|
Description: |
| | Function Constructs are a group of statements that specifies the beginning and end of section of code that forms the body of a function. |
Syntax: |
| The following shows the formats of the Function Construct syntax.
| Visual Basic & BASIC | C & Java |
Function <funname>(<arglist>) As <type>
<statements>
[Exit Function]
[<funname>=<returnvalue>]
End Function | Function <type> <funname>(<arglist>)
{
<statements>
return [<returnvalue>];
}
BUG: return (); does not work in 5.0.1 compiler.
Use the Basic syntax <funname> = <returnvalue>; instead |
The following table lists the elements of the Function Constructs syntax.
| Element | Description |
| Basic | C & Java |
| Function | Function | Function Construct beginning keyword. C and Java still need the Function heading to denote a function returning a value. |
| <funname> | <funname> | Name of the function. Follows standard naming conventions. |
| <arglist> | <arglist> | A list of Variables, and their names, that are used for passing values to the Function. The format of the argument list is shown below. NOTE: This element is only available in ooPIC Compiler Version 3.0 and greater. |
| <type> | <type> | The type of value to pass back to the calling expression. May be one of the following; Byte and Word. Earlier compilers used objects for return values and argument lists, current compilers expect variables, not objects. |
| <statements> | <statements> | The statements that are executed. |
| Exit Function | | Optional Function Construct termination keyword. Can occur any number of times within the <statements> |
| | return | Function Construct keyword that specifies the value to return |
| <returnvalue> | <returnvalue> | The value that this function returns. |
| End Function | | Function Construct ending keyword. |
The following shows the format of the <arglist> element.
| Basic | C & Java |
([<id1> As <type1>[, <idX> As <typeX>[,...]]]) | ([<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> | The type of Variable to use. May be one of the following; Byte, Word. |
| <idX> | <idX> | The name of subsequent optional identifiers. Follows standard naming conventions. |
| <typeX> | <typeX> | The type of Variable to use for each subsequent identifier. May be one of the following; Byte, Word. |
|
Operation: |
| When a function is called during the program's execution, the program flow's current position is saved on the Program-Stack and the program flow will then branch to the function specified by the <funname> element. After branching to the function, the program flow executes the statements within the <statements> element until either the end of the Function Construct or the optional termination keyword is encountered, at which time the original program flow position is retrieved off of the Program-Stack and the program flow is returned to its original position with the value specified by <returnvalue>. The following rules apply to functions.
|
Remarks: |
| | All executable code must be in either procedures or functions. You cannot define either procedures or functions inside another procedure or function. Caution: Functions 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 programs, the program flow branches to and return from a function named "GetMyVal".
| Basic | C & Java |
Dim SW As New oDIO1
Dim A1 As New oA2D
Dim A2 As New oA2D
Dim P As New oDIO8
Sub Main()
Call SetUp
Do
P = GetMyVal
Loop
End Sub
Function GetMyVal() As Byte
If SW.Value = cvPressed Then
GetMyVal = A1.Value
Else
GetMyVal = A2.Value
End If
End Function
Sub SetUp()
SW.IOLine = 31
SW.Direction = cvInput
A1.IOLine = 1
A1.Operate = cvTrue
A2.IOLine = 2
A2.Operate = cvTrue
P.IOGroup = 1
P.Direction = cvOutput
End Sub | oDIO1 SW = New oDIO1;
oA2D A1 = New oA2D;
oA2D A2 = New oA2D;
oDIO8 P = New oDIO8;
Sub Void Main(Void)
{
SetUp;
Do
{
P = GetMyVal;
} While (1 == 1);
}
Function Byte GetMyVal(Void)
{
If (SW.Value == cvPressed)
{
Return A1.Value;
}
Else
{
Return A2.Value;
}
}
Sub Void SetUp(Void)
{
SW.IOLine = 31;
SW.Direction = cvInput;
A1.IOLine = 1;
A1.Operate = cvTrue;
A2.IOLine = 2;
A2.Operate = cvTrue;
P.IOGroup = 1;
P.Direction = cvOutput;
} |
In the following example, a Function Construct is shown that takes two values as arguments, and then passes a Byte when finished.
Function GetMyVal(V1 As Byte, V2 as Word) As Byte | Function Byte GetMyVal(Byte V1, Word V2) |
|
Related Items:
|
| | Procedure Call |
Version History and Bug List: |
| | Firmware Ver A1: Introduced. Bugs: No known bugs. |
|