• <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>

Function Statement

Format
function ʃreturn_typeʅ function_name(ʃparameter1_type parameter1_name, ...,ʅ
                                     
ʃparameterN_type parameterN_nameʅ)

   
// statements

   
ʃfunction_name = return_value;ʅ

end;
Description
The function statement defines a user-defined function with the name function_name. Once defined, the function can be called in other user-defined functions or in procedures throughout your application.
Numeric expressions, string expressions, objects (such as arrays and file handlers), and function pointers can be passed to a user-defined function as arguments. These parameters are defined by specifying the variable type (e.g., parameter1_type) and the name of the variable (e.g., parameter1_name). If no type is specified, the variable will be considered numeric. Preceding the parameter type with the optional keyword marks the parameter as an optional parameter for which a caller does not need to supply a corresponding argument.
The names used in the parameter list of a function are local to the function. They may not be the same as names that are defined in any dictionary or in PROC GLOBAL, but the names can be reused in other functions or procedures.
Numeric, string, and alphanumeric variables are local to the function. That is, if a variable is passed as an argument, its value in the rest of the application will not be changed by actions within the function (this is called "pass by value"). On the other hand, objects (such as arrays and file handlers) passed as arguments refer to the source variable and interactions on the variable affect the source variable (this is called "pass by reference"). If you want to pass a numeric or string variable by reference, you can use the ref keyword to signify that changes made in the function should affect the source variable.
Other than arrays, parameters are defined the same way you would in other parts of logic. However, with arrays, you do not define the size of the array because the array will match the size of the array passed as an argument. By default, an array is one-dimensional. To specify more than one dimension, use parentheses with the number of dimensions specified using commas. For example, a three-dimensional array must include (,,) after the parameter name. Within the function, the function length returns the dimension sizes of the passed array.
Functions always return a value, either a numeric, alphanumeric, or string value. If return_type is not specified, the function will by default return a numeric value. To assign the function's return value, assign a value to function_name. If no return value is specified, the value default or a blank string is returned, depending on the return type. The return value can also be specified using the exit statement.
When using functions in logic, CSPro will check that the function call is valid at during compilation. However, advanced users may find it useful to program function calls where the function name is only known at runtime. You can use the invoke function to execute such functions using runtime binding.
Example 1
PROC GLOBAL

function CompareValues(numeric value1, numeric value2)

   
if value1 <> value2 then
       
errmsg("The values entered, %d and %d, are not equal.", value1, value2);
       
reenter;
   
endif;

end;

PROC BIRTH_DATE

    CompareValues(BIRTH_DATE, BIRTH_DATE_FROM_PREVIOUS_ROUND);
Example 2
PROC GLOBAL

function string GetNameOfFirstFemale()

   
numeric female_index = seek(SEX = 2);

   
if female_index > 0 then
        GetNameOfFirstFemale = NAME(female_index);
   
endif;

end;
Example 3
PROC GLOBAL

function numeric SumList(List numeric numeric_list)

   
numeric summed_value = 0;

   
do numeric counter = 1 while counter <= length(numeric_list)
       
inc(summed_value, numeric_list(counter));
   
enddo;

    SumList = summed_value;

end;
Example 4
PROC GLOBAL

function string CapitalizeFirstLetters(string text)

   
do numeric counter = 1 while counter <= length(text)

       
if counter = 1 or text[( counter - 1 ):1] = " " then
            text[counter:
1] = toupper(text[counter:1]);
       
endif;

   
enddo;

    CapitalizeFirstLetters = text;

end;
See also: User-Defined Functions, Optional Function Parameters, Passing Function Arguments by Reference, Additional Examples of User-Defined Functions, Exit Statement