Cicode Programming Reference > Writing Functions > Function Argument Structure

Function Argument Structure

The optional Arguments Statement follows the required FUNCTION Statement and precedes the executable statements of a function in Cicode.

Note: The maximum number of arguments you can have in a function is 128.

When you call a function, you can pass one or more arguments to the function, enclosed within the parentheses ( ) located after the function name statement. Replace the <Arguments> placeholder in the following function example with your Argument Statement.

FUNCTION
FunctionName ( <Arguments> )
<Statement> ;
<Statement> ;
<Statement> ;
END

For your function to perform tasks with data, it requires accessibility to the data. One way to achieve this, is to pass the data directly to the function when the function is being called. To enable this facility, Cicode utilizes arguments in its function structure. An argument in Cicode is simply a variable that exists in memory only as long as its function is processing data, so the scope of an argument is limited to be local only to the function. Arguments cannot be arrays.

Arguments are variables that are processed within the body of the function only. You cannot use an argument outside of the function that declares it.

As arguments are variables used solely within functions, they needs to be declared just as you would otherwise declare a variable in Cicode. See the section titled Declaring Variable Properties. An argument declaration requires a data type, a unique name, and may contain an initial value which also behaves as the default value for the argument.

Notes: In the following function syntax example:

Cicode function argument statements have the following syntax:

<ArgumentDataType>
<ArgumentName>
[ = <InitialDefaultValue> ]

where:

The Argument Statement in a Cicode function can have only one set of surrounding parentheses ( ), even if no arguments are declared in the function.

If more than one argument is used in the function, each needs to also be separated by a comma.

Argument Statements can be separated over several lines to aid in their readability.

When you call a function, the arguments you pass to it are used within the function to produce a resultant action or return a value. For information on passing data to functions, see the section titled Passing Data to Functions (Arguments). For information on returning results from functions, see the section titled Returning Data from Functions.

Arguments are used in the function and referred to by their names. For instance, if we name a function AddTwoIntegers, and declare two integers as arguments naming them FirstInteger and SecondInteger respectively, we would end up with a sample function that looks like the following:

INT
FUNCTION
AddTwoIntegers ( INT FirstInteger, INT SecondInteger )
INT Solution ;
Solution = FirstInteger + SecondInteger ;
RETURN Solution ;
END

In this example, the function would accept any two integer values as its arguments, add them together, and return them to the caller as one integer value equal to the summed total of the arguments values passed into the function.

This functionality of passing values into a function as arguments, manipulating the values in some way, then being able to return the resultant value, is what makes functions potentially very powerful and time saving. The code only needs to written once in the function, and can be utilized any number of times from any number of locations in CitectSCADA. Write once, use many.