Difference between revisions of "Function calls and parameters"

m (hyperlinks to functions used in the examples)
m
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
  
  
Analytica provides a large number of built-in functions for performing mathematical [[Math functions]], [[Array Functions and Operators|array]], [[Statistical functions|statistical]],  [[Text, Date, Math, and Financial Functions]]. There are also [[Probability Distributions|probability]] distribution functions for [[uncertainty]] and [[Sensitivity analysis functions|sensitivity analysis]]. The Enterprise edition of Analytica also includes database [[Database functions|functions]]. Finally, you can write and use your own user-defined functions (see [[User-defined Functions and Libraries]]).
+
Analytica provides a rich set of built-in functions including [[Math functions]], [[Array Functions and Operators|array]], [[Statistical functions|statistical]],  [[Text, Date, Math, and Financial Functions]], [[Probability Distributions]], [[Spreadsheets and Analytica|spreadsheet functions]] and more. You can also write and use your own [[User-defined Functions and Libraries]]).
  
Here are some simple examples of expressions involving functions.
+
Here are some simple expressions involving functions.
 
:<code>[[Exp]](1) &rarr; 2.718281828459</code>
 
:<code>[[Exp]](1) &rarr; 2.718281828459</code>
:<code>[[Sqrt]](3^2 + 4^2) &rarr; 5 [[Mod]](7, 3) &rarr; 1</code>
+
:<code>[[Sqrt]](3^2 + 4^2) &rarr; 5 </code>
:<code>[[Pmt]](8%, 30, -1000) &rarr; $88.83</code>
+
:<code>[[Mod]](7, 3) &rarr; 1</code>
:<code>[[Normal]](500, 100)</code>
+
:<code>[[Normal]](mean: 500, sdeviation: 100)</code>
 +
:<code>[[FindInText]]('ONE', 'Result is one or two.', caseInsensitive: True, return: 'S') &rarr; 'one'</code>
  
'''Position-based function calls''': With this conventional syntax, you just list the parameters in sequence between parentheses after the function name:
+
You can see
 +
==Position-based function calls==
 +
 +
With this conventional syntax, you just list the parameters after the function name between parentheses in the same sequence that they are defined for the function:
 
:<code>FunctionName(param1, param2, ...)</code>
 
:<code>FunctionName(param1, param2, ...)</code>
  
In most cases, each parameter can be an [[expression]] built out of constants, variable names, operators, and function calls and so on.  
+
In a function call, each parameter may be a constant, name of a variable, or a more complex expression with operators, or function calls. In some cases, a parameter must be of an expected type -- for example, the second parameter of Sum must be an index to sum over:
 +
:<code>[[Sum]](Cash_by_year, Year)</code>
  
You can omit any optional parameters if you want them to use their default value.
+
Some parameters are [[Optional parameter|optional]], in which case you may omit them and the function will use default values.
  
'''Name-based function calls''': With this syntax, you name each parameter, followed by a colon (:) and the value passed to that parameter. Since the parameters are named, you can list them in any order. For example, this function has four parameters, of which you can provide any two to define the distribution:
+
== Name-based function calls==
 +
 
 +
With name-based syntax to call a function, you name each parameter, followed by a colon (:) and the value passed to that parameter. You may list the parameters in any order, and omit any optional parameters. For example, [[LogNormal]] has four parameters, of which you may provide any two to define the distribution:
 
:<code>[[LogNormal]](median, gsdev, mean, stddev)</code>
 
:<code>[[LogNormal]](median, gsdev, mean, stddev)</code>
  
Calling it using name-based syntax:
+
You can call it using name-based syntax:
 
:<code>[[LogNormal]](mean: 10, stddev: 1.5)</code>
 
:<code>[[LogNormal]](mean: 10, stddev: 1.5)</code>
  
is equivalent to the following using position-based syntax, which uses commas to indicate that the first two parameters are omitted:
+
This is equivalent to this position-based syntax, using extra commas to indicate omitted parameters:
 
:<code>[[LogNormal]]( , , 10, 1.5)</code>
 
:<code>[[LogNormal]]( , , 10, 1.5)</code>
  
because <code>mean</code> and <code>stddev</code> (standard deviation) are the third and fourth parameters. Name-based syntax is useful for functions with many optional parameters. It’s usually easier to read name-based function calls because you don’t need to remember the ordering of the parameters.
+
Name-based syntax is useful for functions with many optional parameters, since you don't have to count commas and remember the order of the parameters. It’s usually easier to read name-based function calls.
 +
 
 +
You may mix position- and named-based syntax if you use position for the initial parameters:
 +
:<code>[[LogNormal]](10, stddev: 4)</code>
 +
You may not  use a position-based parameter after a name-based parameter:
 +
:<code>[[LogNormal]](mean: 10, 1.5) &rarr; ''syntax error'' </code>
  
 
==See Also==
 
==See Also==

Latest revision as of 20:51, 29 December 2017



Analytica provides a rich set of built-in functions including Math functions, array, statistical, Text, Date, Math, and Financial Functions, Probability Distributions, spreadsheet functions and more. You can also write and use your own User-defined Functions and Libraries).

Here are some simple expressions involving functions.

Exp(1) → 2.718281828459
Sqrt(3^2 + 4^2) → 5
Mod(7, 3) → 1
Normal(mean: 500, sdeviation: 100)
FindInText('ONE', 'Result is one or two.', caseInsensitive: True, return: 'S') → 'one'

You can see

Position-based function calls

With this conventional syntax, you just list the parameters after the function name between parentheses in the same sequence that they are defined for the function:

FunctionName(param1, param2, ...)

In a function call, each parameter may be a constant, name of a variable, or a more complex expression with operators, or function calls. In some cases, a parameter must be of an expected type -- for example, the second parameter of Sum must be an index to sum over:

Sum(Cash_by_year, Year)

Some parameters are optional, in which case you may omit them and the function will use default values.

Name-based function calls

With name-based syntax to call a function, you name each parameter, followed by a colon (:) and the value passed to that parameter. You may list the parameters in any order, and omit any optional parameters. For example, LogNormal has four parameters, of which you may provide any two to define the distribution:

LogNormal(median, gsdev, mean, stddev)

You can call it using name-based syntax:

LogNormal(mean: 10, stddev: 1.5)

This is equivalent to this position-based syntax, using extra commas to indicate omitted parameters:

LogNormal( , , 10, 1.5)

Name-based syntax is useful for functions with many optional parameters, since you don't have to count commas and remember the order of the parameters. It’s usually easier to read name-based function calls.

You may mix position- and named-based syntax if you use position for the initial parameters:

LogNormal(10, stddev: 4)

You may not use a position-based parameter after a name-based parameter:

LogNormal(mean: 10, 1.5) → syntax error

See Also


Comments


You are not allowed to post comments.