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, within parentheses, and separated by commas, in the same sequence in which they are defined. For example:
+
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, identifying the parameters by name:
+
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, we name each parameter, and put its actual value after a colon “:” after the parameter name. The name-value pairs are separated by commas. You can give the parameters in any order. They 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]].
+
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, provided the positional parameters come first:
+
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 cannot give a positional parameter after a named parameter. For example, the following entry displays an error message:
+
You may not give a positional parameter after a named parameter:
  
:<code>Fu1(1, D: 4, 2, 3) Invalid</code>
+
:<code>Fu1(1, D: 4, 2, 3) → Syntax error</code>
  
This ''name-based calling syntax'' is analogous to Analytica’s ''name-based subscripting'' for arrays to obtain selected elements of an array, in which you specify indexes by name. You don’t have to remember a particular sequence to write or understand an expression. See [[Subscript_and_slice_of_a_subarray#x.5Bi_.3D_v.5D:_Subscript_construct|x[i=v]: Subscript construct]].
+
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.

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.

See Also


Comments


You are not allowed to post comments.