• <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
        • declare 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
      • Numeric Values
      • String Values
      • 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
      • StringWriter 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
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

declare Variable Modifier

Format
declare variable_declaration;
Description
The declare keyword instructs the compiler to compile the rest of the statement as a variable declaration, not a definition. A declaration provides information to the compiler about a variable, specifying its name and type, but does not actually define the variable. Once declared, the variable can be used throughout logic prior to its definition. The variable can be declared repeatedly but must be defined only once. The variable type used at definition must match that used at declaration.
As of CSPro 8.1, the declare keyword only works with CSPro logic or JavaScript user-defined functions that use the variable type function. This is useful as a way to use a function in other functions without worrying about the order that the functions are defined in the logic file.
When declaring a function, it is not necessary to name each parameter, and if a parameter name is provided, it does not need to match the name used at definition.
For CSPro logic declarations, a compiler error occurs if the variable is never defined.
For JavaScript function declarations, the function's existence is not known until runtime. If the function does not exist when called, a runtime error is displayed and the function returns default or a blank string, depending on the return type.
Example (Forward Declaring)
// declare two functions
declare function string GetRandomString(Array string);

declare function string GetStateInformation(numeric parameter_names_are_optional, string, Array string);

// this function will use GetStateInformation prior to it being defined
function DisplayPennsylvaniaInformation()

   
Array string pa_cities(3) = "Philadelphia", "Pittsburgh", "Punxsutawney";

   
// displays text similar to: Pennsylvania was founded in 1681. The state has famous cities including Punxsutawney.
    errmsg("%s", GetStateInformation(1681, "Pennsylvania", pa_cities));

end;

// this function defines a previously declared function and it uses GetRandomString prior to it being defined
function string GetStateInformation(numeric founding_year, string state_name, Array string city_names)

   
exit maketext("%s was founded in %d. The state has famous cities including %s.",
                  state_name, founding_year, GetRandomString(city_names));

end;

// this function defines a previously declared function
function string GetRandomString(Array string values);

   
seed(systime());
   
exit values(random(1, values.length()));

end;
Example (JavaScript Function Wrapper)
PROC GLOBAL

// declare that this CSPro function will call a JavaScript function called "sign"
declare function numeric JS sign(numeric);

PROC EXAMPLE

   
// add "sign" as a JavaScript function that wraps JavaScript's Math.sign function
    JS.eval("function sign(value) { return Math.sign(value); }");

   
// use the function as if it were a CSPro function, but
    // when called it will execute the JavaScript function

   
errmsg("%d %d %d", sign(123),  //  1
                       sign(-975), // -1
                       sign(0));   //  0
See also: User-Defined Functions, JavaScript User-Defined Functions