Difference between revisions of "Call a function"
m |
|||
Line 3: | Line 3: | ||
<breadcrumbs>Analytica User Guide > User-defined Functions and Libraries > {{PAGENAME}}</breadcrumbs><br /> | <breadcrumbs>Analytica User Guide > User-defined Functions and Libraries > {{PAGENAME}}</breadcrumbs><br /> | ||
− | ==Position-based calling == | + | == Position-based syntax for calling a function == |
− | Analytica uses the standard position-based syntax for using, or calling, a function. You simply list the actual parameters after the function name, | + | Analytica uses the standard position-based syntax for using, or calling, a function. You simply list the actual parameters in the standard sequence after the function name, separated by commas, and between parentheses. For example: |
:<code>Capm(5%, 8%, StockBeta)</code> | :<code>Capm(5%, 8%, StockBeta)</code> | ||
Line 11: | Line 11: | ||
This evaluates function [[Capm]](Rf, Rm, Beta) with <code>Rf</code> set to 5%, <code>Rm</code> set to 8%, and <code>Beta</code> set to <code>Stockbeta</code>. | This evaluates function [[Capm]](Rf, Rm, Beta) with <code>Rf</code> set to 5%, <code>Rm</code> set to 8%, and <code>Beta</code> set to <code>Stockbeta</code>. | ||
− | ==Name-based calling== | + | ==Name-based syntax for calling a function == |
− | Analytica also supports a more flexible name-based calling syntax, | + | Analytica also supports a more flexible name-based calling syntax. As you might guess, you identify each parameter by name: |
:<code>Capm(beta: StockBeta, rf: 5%, rm: 8%)</code> | :<code>Capm(beta: StockBeta, rf: 5%, rm: 8%)</code> | ||
− | In this case, | + | In this case, you give the name of each parameter, followed by colon “:”, and an expression whose value is passed to that parameter. As with position-based syntax, the parameters are separated by commas and between parentheses. But with name-based syntax, the parameters can appear in any sequence. It must include all required parameters. This method is much easier to read when the function has many parameters. It is especially useful when many [[Parameter_qualifiers#Optional_parameters|parameters are optional]]. |
− | You can mix positional and named parameters, | + | You can mix positional and named parameters, but you must put the positional parameters first: |
:<code>Fu1(1, 2, D: 4, C:3)</code> | :<code>Fu1(1, 2, D: 4, C:3)</code> | ||
− | You | + | You may not give a positional parameter after a named parameter: |
− | :<code>Fu1(1, D: 4, 2, 3) | + | :<code>Fu1(1, D: 4, 2, 3) → Syntax error</code> |
− | This | + | This name-based calling syntax is analogous to Analytica’s ''name-based subscripting'' for arrays: To select a cell or slice of a multi-dimensional array, you specify each index by name, along with the selection value -- so you can specify them in any sequence, which makes subscript expressions much easier to write and more robust. See [[Subscript_and_slice_of_a_subarray#x.5Bi_.3D_v.5D:_Subscript_construct|x[i=v]: Subscript construct]]. |
<Tip Title="Tip">Name-based calling syntax works for all user-defined functions. It also works for most of the builtin functions, except for a few with only one or two parameters.</Tip> | <Tip Title="Tip">Name-based calling syntax works for all user-defined functions. It also works for most of the builtin functions, except for a few with only one or two parameters.</Tip> |
Latest revision as of 05:17, 29 November 2016
Position-based syntax for calling a function
Analytica uses the standard position-based syntax for using, or calling, a function. You simply list the actual parameters in the standard sequence after the function name, separated by commas, and between parentheses. For example:
Capm(5%, 8%, StockBeta)
This evaluates function Capm(Rf, Rm, Beta) with Rf
set to 5%, Rm
set to 8%, and Beta
set to Stockbeta
.
Name-based syntax for calling a function
Analytica also supports a more flexible name-based calling syntax. As you might guess, you identify each parameter by name:
Capm(beta: StockBeta, rf: 5%, rm: 8%)
In this case, you give the name of each parameter, followed by colon “:”, and an expression whose value is passed to that parameter. As with position-based syntax, the parameters are separated by commas and between parentheses. But with name-based syntax, the parameters can appear in any sequence. It must include all required parameters. This method is much easier to read when the function has many parameters. It is especially useful when many parameters are optional.
You can mix positional and named parameters, but you must put the positional parameters first:
Fu1(1, 2, D: 4, C:3)
You may not give a positional parameter after a named parameter:
Fu1(1, D: 4, 2, 3) → Syntax error
This name-based calling syntax is analogous to Analytica’s name-based subscripting for arrays: To select a cell or slice of a multi-dimensional array, you specify each index by name, along with the selection value -- so you can specify them in any sequence, which makes subscript expressions much easier to write and more robust. See x[i=v]: Subscript construct.
See Also
- Function calls and parameters
- Parameter qualifiers
- Function parameter qualifiers
- User-defined functions
Enable comment auto-refresher