• <Helps for GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
      • Array Object
      • Audio Object
      • Barcode and QR Codes
      • 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
        • Compress Function
        • Decompress Function
        • Diagnostics Function
        • Encode Function
        • ErrMsg Function
        • ExecSystem Function (Desktop)
        • ExecSystem Function (Mobile)
        • ExecPFF Function
        • GetProperty Function
        • GetLabel Function
        • GetLanguage Function
        • GetSymbol Function
        • GetValueLabel Function
        • Hash Function
        • HtmlDialog Function
        • InValueSet Function
        • Invoke Function
        • IsChecked Function
        • LoadSetting Function
        • LogText Function
        • MaxValue Function
        • MinValue Function
        • Paradata Function
        • PathConcat Function
        • PathName Function
        • Report Function (Deprecated)
        • SaveSetting Function
        • SetLanguage Function
        • SetProperty Function
        • SetReportData Function (Deprecated)
        • SetValueSet Function
        • SetValueSet Function With Arrays
        • SetValueSets Function
        • Special Function
        • SqlQuery Function
        • Stop Function
        • SysParm Function
        • Tr Function
        • Trace Function
        • UUID Function
        • View Function
        • Warning Function
      • Date and Time Functions
      • External File Functions
      • Synchronization Functions
      • Export Attributes
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Appendix
  • <Helps for CSEntry>
  • <Helps for CSBatch>
  • <Helps for CSTab>
  • <Helps for DataViewer>
  • <Helps for TextView>
  • <Helps for TblView>
  • <Helps for TRSWin>
  • <Helps for CSDeploy>
  • <Helps for CSPack>
  • <Helps for CSFreq>
  • <Helps for CSSort>
  • <Helps for CSExport>
  • <Helps for CSReFmt>
  • <Helps for CSDiff>
  • <Helps for CSConcat>
  • <Helps for TRSSetup>
  • <Helps for ParadataViewer>
  • <Helps for ParadataConcat>
  • <Helps for CSIndex>
  • <Helps for Excel2CSPro>
  • <Helps for CSWeb>

Invoke Function

Format
s = invoke(function_nameʃ, argument1, ..., argumentNʅ);
s = invoke(function_name, arguments := arguments_json);
Description
The invoke function allows you to execute user-defined functions using runtime binding (where the function name is only known at runtime). When compiling an application, the CSPro compiler only knows about functions declared up to the point where a function call is coded, so for more dynamic applications, it can be a challenge to manage function calls when you would like to call a function declared after the point of the call. One workaround is to use function pointers; the invoke function provides another way to bypass the CSPro compiler limitation.
The invoke function evaluates the string expression function_name at runtime, not compile-time. In the function's first version, zero, one, or more arguments are provided directly at compile-time. At runtime, the arguments are checked against the function's parameters, and if they are compatible, the function call is executed.
The function's second version, using named arguments, allows arguments to be passed using the string expression arguments_json. This text is a JSON string containing the arguments to the function. In this version, only numeric, string, and list arguments can be provided. List arguments must be provided as JSON arrays.
The JavaScript interface provides versions of these functions that can be run in JavaScript: CSPro.invoke and CSPro.invokeAsync.
Return Value
The function returns the return value of the user-defined function as a string when successful, and a blank string otherwise. If the function has a numeric return value, the value is converted to a string, which you can convert back to a number using the tonumber function.
Example 1
function numeric ManipulateValues(numeric value1, numeric value2,
                                 
string function_name = "AddValues")

   
string result = invoke(function_name, value1, value2);
   
exit tonumber(result);

end;

function AddValues(numeric value1, numeric value2)
   
exit value1 + value2;
end;

function MultiplyValues(numeric value1, numeric value2)
   
exit value1 * value2;
end;

// ...

ManipulateValues(
16, 16);                   // 32
ManipulateValues(16, 16, "MultiplyValues"); // 256
ManipulateValues(16, 16, "DivideValues");   // runtime error (DivideValues function does not exist)
invoke("ManipulateValues", 16, 16);         // "32"
Example 2
function string CombineText(list string values)

   
string result;

   
do numeric ctr = 1 while ctr <= values.length()
        result = result + values(ctr) + 
" ";
   
enddo;

   
exit strip(result);

end;

// ...

invoke("CombineText", arguments := '{ "values": [ "Hello,", "World!" ] }'); // Hello, World!
See also: User-Defined Functions, JavaScript Interface