AnyLogic enables defining your own functions. Function will return the value of an expression each time the user calls it from the model. Functions are helpful when you need to re-use the same function in multiple places in your model. Since you write functions in Java, you have all the advantages of this language, such as conditional and cyclic operators, branches and more.

AnyLogic supports action charts - structured block charts allowing defining algorithms graphically in the style of structured programming. Using action charts, you can define algorithms even if you are not familiar with the syntax of Java operators. Another evident benefit of using action charts is visualizing the implemented algorithm, thus making it more intuitive to other users.

AnyLogic also supports a special type of functions - table functions. A table function is a function defined in the tabular form. You can make it continuous by interpolating and/or extrapolating. You may need table functions to define a complex non-linear relationship which cannot be described as a composition of standard functions, or to bring experimental data defined as a table function to a continuous mode.

 To define a function
  1. Drag the  Function element from the  Agent palette onto the graphical diagram of agent (or experiment).
  2. In the Properties view, type the name of the function in the Name box. 
  3. If your function only performs some actions but does not return any result of the calculations, specify that this function returns nothing by choosing Just action (returns nothing) from the group of buttons. 
  4. Otherwise, if you need to obtain the result of calculations, select Returns value. Unlike many other simulation tools, in AnyLogic user-defined function can return a value of absolutely any Java class.
  5. You can choose one of the most-used types (boolean, int, double, String) using the corresponding choice from the Type drop-down list. However, if you need to define a function returning the value of some other Java class, choose Other option and type the required class name in the edit box to the right.
  6. Open the Function body section of the function properties and type the function expression in the field. The final line of the expression should be the return expression:
    return <expression that returns the value of the specified return type>;



Name – The name of the function. The name is used to identify and access the function.

Show name – If selected, the function name is displayed on a presentation diagram.

Ignore – If selected, the function is excluded from the model.

Visible – If selected, the function is visible on the presentation at runtime.

Just action (returns nothing) – If selected, the function just performs some actions and returns nothing.

Returns value – If selected, the function returns the result of calculations. You can specify the type of the function's return value in the Type field below. 

Type – [Visible if the option Returns value is selected] Here you can specify the type of the function's return value. A function can return a value of some most-used types (boolean, intdoubleString), or a value of some Other Java class (in this case specify the Java class name in the field to the right).


Here you can define some optional arguments for your function to enable passing some additional input data necessary for function calculations.
Each row of the table specifies one particular argument. To remove an argument, select the corresponding row in the table and click the "cross" button. Rearrange arguments in the table using the "arrow" buttons.

Function body

Type here the function body. If the function returns some value, the final line should be the return expression.


Static – If selected, the function is static. A static function does not need creating any instances of the agent type, in whose code it is defined. Static function defined in the MyClass agent type can be accessed from any place of the model as MyClass.function().

Access – The access type of the function. There are four access types:
    public - the function can be accessed from anywhere 
    private – the function can be accessed from this agent class only
the function can be accessed from this agent class and its subclasses
the function can be accessed anywhere within this model. 

System dynamics units – If selected, you will be able to specify measurement units for the value returned by this function in the edit box to the right. Having specified units for elements of your model, you may perform unit checking to find out dimension inconsistencies in the model. 

Function arguments

You may define some optional arguments for your function to enable passing some additional input data necessary for function calculations.

 To define arguments of the function
  1. Select the function in the graphical editor.
  2. On the Arguments section of the Properties view, define parameters in the function arguments table.
  3. Each row of the table specifies one particular parameter.
  4. In the Name cell of the table, type the parameter name.
  5. In the Type cell of the table, specify the type of the parameter. Click in the cell and choose the required type from the drop-down list or type in the cell.

If you have defined any parameters for the function, you must pass parameter values any time you call the function. The values must be provided in the order they are defined in the function arguments table.

Since the order of parameters in the table is significant, AnyLogic enables changing the parameter order just moving them up or down using the buttons  and . To remove a parameter, select the parameter row in the table and click the  button.

Calling functions

To call a function, write the function name followed by parentheses. If the function has arguments, you need to pass the argument values separated by commas inside the parentheses. The values must be provided in the order they are defined in the function arguments table in the Arguments section of the function properties.



moveTo(15, 27);

Functions access scope

By default, a function can be called from any place of your model. You can restrict the access scope for the function by changing the function’s access type. Choose the required access type using the Access drop-down list in the function's properties Advanced section.

Related topics

 Table functions