Functions

Functions are declared with the function keyword, they can be passed Arguments and can Return Values.

Function names must start with a letter or underscore and cannot use any Reserved Words.

          // Declaring a function
function calcSum (x,y) { 
    return x + y;
}

// Calling a function
var result = calcSum(15,25); 

        
Note:

Variables declared inside a function as var are local to the function. Variables defined inside a function without the var are global variables.

Arguments

Functions do not need arguments.

          function validateTravelDates() {
   var receiptDate = NSOA.form.getValue('date');
   var travelDate = NSOA.form.getValue('TravelDate__c');
   
   if ( receiptDate < travelDate ) {
      NSOA.form.error('TravelDate__c', 'The travel date cannot be after the receipt date!');
   }
} 

        

You can pass as many arguments as you like separated by commas.

          function calcSum (x,y,z) { 
    var result = x + y + z;
    return result;
} 

        

See also:

Important:

If you declare a function with arguments then the function must be called with all the arguments in the expected order.

Return Values

Functions do not need to return a value.

          function logFormError(message) {
    NSOA.meta.log('error', 'Form error - ' + message);
} 

        

See also:

Use the return statement to return a variable from a function.

          function calcProduct (x,y) { 
    var result = x * y;
    return result;
} 

        

You can use the return statement to immediately exit a function. The return value is optional.

          function reduceValue (x,y) { 
    if ( x < y ) {
        return; // exit function
    }

    var result = x - y;
    return result;
}