Cicode Programming Reference > Writing Functions > Setting Default Values for Arguments

Setting Default Values for Arguments

If an argument is listed in a Cicode function declaration, the Argument Initialisation Statement is optional, and if used, is listed last in the Argument Statement after the required Argument Data Type and the Argument Name Statements. The Argument Initialization Statement needs to be preceded by an equals ( = ) assignment operator.

Note: In the following function syntax example:

  • Every placeholder shown inside arrow brackets ( <placeholder> ) should be replaced in any actual code with the value of the item that it describes. The arrow brackets and the word they contain should not be included in the statement, and are shown here only for your information.
  • Statements shown between square brackets ( [ ] ) are optional. The square brackets should not be included in the statement, and are shown here only for your information.
  • Replace the <InitialDefaultValue> placeholder in the following function example with an appropriate value for your Argument variable.

    FUNCTION
    FunctionName (<ArgumentDataType> <ArgumentName> [ = <InitialDefaultValue> ])
    <Statement> ;
    <Statement> ;
    <Statement> ;
    END

    The default value for an argument needs to be of the same data type as declared for the argument in the Argument Data Type Statement.

    You assign a default argument variable value in the same manner that you assign a Cicode variable value, by using the equals ( = ) assignment operator. For example:

    FUNCTION
    PlotProduct ( INT iPackets = 200 , STRING sName = "Packets" )
    <Statement> ;
    <Statement> ;
    <Statement> ;
    END

    If you assign a default value for an argument, you may omit a value for that argument when you call the function, because the function will use the default value from the declaration. To pass an empty argument to a function, omit any value for the argument in the call. For example, to call the PlotProduct function declared in the previous example, and accept the default string value of "Packets", a Cicode function call would look like:

    PlotProduct(500)
    

    Be aware that the second argument for the function was omitted from the calling code. In this instance, the default value for the second argument ( "Packets" ) would remain unchanged, and so would be used as the second argument value in this particular function call.

    If you do call that function and pass in a value for that argument in the call, the default value is replaced by the argument value being passed in. However, the arguments are reinitialized every time the function is called, so each subsequent call to the function will restore the default values originally declared in the function.

    If the function has more than one argument and none of them are explicitly declared in the function declaration, the default values for the undeclared arguments will be used.

    For more information on function calls, callers, and calling, see the section titled Calling Functions from Commands and Expressions.

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

    See Also

    Published June 2018