• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
        • Numeric Statement
        • String Statement
        • Alpha Statement
        • config Variable Modifier
        • ensure Variable Modifier
        • persistent Variable Modifier
        • Visual Values for Numeric Fields
        • Relation Statement
        • Function Named Arguments
        • Function Statement
        • Optional Function Parameters
        • Passing Function Arguments by Reference
        • Additional Examples of User-Defined Functions
        • Dot Notation, Logic Objects, and Namespaces
      • Symbol Functions
      • Item Functions
      • Array Object
      • Audio Object
      • Barcode and QR Codes
      • Case Object
      • Document Object
      • File Object
      • Freq Object
      • Geometry Object
      • HashMap Object
      • Image Object
      • List Object
      • Map Object
      • Path
      • Pff Object
      • SystemApp Object
      • ValueSet Object
      • Program Control Statements
      • Assignment Statements
      • Data Entry Statements and Functions
      • Batch Edit Statements
      • Numeric Functions
      • String Functions
      • Multiple Occurrence Functions
      • General Functions
      • Date and Time Functions
      • External File Functions
      • Synchronization Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Optional Function Parameters

Format
function FunctionName(optional numeric parameter1, optional string parameter2)

function FunctionName(numeric parameter1 = default_number, string parameter2 = "default string")
Description
User-defined functions can have optional parameters, which you can specify in two ways. The first way is to use the optional keyword before the parameter type. This works with all variable types (numerics, strings, and objects) except for arrays, which cannot be used as optional parameters. The second way is by using an = sign when specifying the parameter. This works for numeric and string parameters only.
Using the Optional Keyword
function MyFunction(optional numeric numeric_value, optional string string_value)
When you call the function, you do not need to supply arguments for optional parameters. If not otherwise defined with the = sign, a number will be set to notappl and a string will be set to a blank string. Any object (such as a HashMap, List, etc.) will be set to its default state (an empty HashMap, a blank List, etc.). Once you specify that a parameter is optional, then all parameters after it must also be optional.
You can call the function like this:
// both arguments specified
MyFunction(3, "ABC");

// one argument specified (string_value set to "")
MyFunction(3);

// no arguments specified (numeric_value set to notappl, string_value set to "")
MyFunction();
Using an = Sign
Using an = sign is for numeric and string parameters only and cannot be used for objects. It is useful for assigning a numeric constant or string literal as the default value:
function MyFunction(optional numeric numeric_value = 30, optional string string_value = "Hello")
When using the = sign, you do not need to use the optional keyword, but you can specify it if you want.
Example 1
PROC GLOBAL

function string GetName(optional numeric person_occurrence)

   
// if the argument for the occurrence number (person_occurrence) is not passed
    // to the function, person_occurrence will be notappl and the function will
    // return the name from the current record; otherwise, person_occurrence will
    // be defined and the function will return the name for the specified record

   
if person_occurrence = notappl then
        person_occurrence = 
curocc(POPULATION_RECORD);
   
endif;

   
exit strip(PERSON_NAME(person_occurrence));

end;


PROC AGE

   
errmsg("%s is %d years younger than the head of household (%s)",
           GetName(), AGE(
1) - AGE, GetName(1));
Example 2
PROC GLOBAL

// This function calculates the age based on the birth date and the enumeration date.
// If the birth date is not passed to the function or is not set, the function returns
// a calculated age of 998 which is used to indicate that the age could not be calculated.
// If the enumeration date is not passed to the function, the function calculates the
// date based on the default date of March 11, 2020; otherwise it uses the date passed
// as a function argument.

function numeric CalculateAge(optional numeric birth_date, optional numeric enumeration_date = 20200311)

   
if special(birth_date) then
       
exit 998;
   
else
       
exit datediff(birth_date, enumeration_date, "y");
   
endif;

end;

// ...

DOB = 
19520218;
ACTUAL_ENUMERATION_DATE = 
20200401;

errmsg("%d", CalculateAge(DOB, ACTUAL_ENUMERATION_DATE)); // 68
errmsg("%d", CalculateAge(DOB));                          // 67
errmsg("%d", CalculateAge());                             // 998
See also: User-Defined Functions, Function Statement