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

Additional Examples of User-Defined Functions

These are examples of more advanced ways to work with user-defined functions in CSPro.
Recursive Functions
function numeric NumberDescendantsInHH(numeric ptrPerson)

   
numeric cntDescendants;

   
do numeric hhCtr = 1 while hhCtr <= totocc(PERSON_REC)

       
if LN_FATHER(hhCtr) = ptrPerson or LN_MOTHER(hhCtr) = ptrPerson then
           
inc(cntDescendants,NumberDescendantsInHH(hhCtr));
       
endif;

   
enddo;

    NumberDescendantsInHH = cntDescendants;

end;
Function Pointers
(The invoke function may provide an alternative to some uses of function pointers.)
function numeric FilteredCountInHH(function filter(numeric))

   
numeric cnt;

   
do numeric hhCtr = 1 while hhCtr <= totocc(PERSON_REC)

       
if filter(hhCtr) then
           
inc(cnt);
       
endif;

   
enddo;

    FilteredCountInHH = cnt;

end;

function numeric IsTeenager(numeric ptrPerson)
    IsTeenager = ( AGE(ptrPerson) 
in 13:19 );
end;

function numeric IsFertileWoman(numeric ptrPerson)
    IsFertileWoman = ( SEX(ptrPerson) = 
2 and AGE(ptrPerson) in 15:49 );
end;


PROC ANALYSIS

   
numeric cntTeenagers = FilteredCountInHH(IsTeenager);
   
numeric cntFertileWomen = FilteredCountInHH(IsFertileWoman);
See also: Function Statement, Optional Function Parameters, Passing Function Arguments by Reference