You can create your own functions and use them where needed. It is recommended to use functions:
- Whenever there are pieces of code that it is likely that you will reuse several times in the questionnaire.
- When you need to refer to values (often computed expressions) that are not stored in survey variables.
- To group statements that perform a well-defined task, to make the code easier to read, and to make your code more modularized.
Functions can be called from anywhere within the questionnaire.
When using the JScript engine, you can utilize strict type annotation in a function. Type annotation in a function specifies the required type for function arguments, the required type for returned data, or both. If you do not type annotate the parameters of a function, the parameters will be of type Object. Similarly, if the return type for a function is not specified, the compiler will infer the appropriate return type.
Using type annotation for function parameters helps ensure that a function will only accept data that it can process. Declaring a return type explicitly for a function improves code readability since the type of data that the function will return is immediately clear.
Note: The JavaScript engine does not have the idea of strict typing, and, as such, cannot utilize these features. Objects returned and input will be the default (and only) JavaScript object type. These types are dynamic and will change based on what is put into the object.