Function#

A function, like inputs and variables, can be thought of as a container. Instead of storing values, this container stores a block of code designed to perform a specific task. A function can accept input parameters, process them, and return a result. Commonly used functionalities are usually written as functions, so there is no need to rewrite the same calculations or features every time — just use the corresponding function name.

Functions, like inputs and variables, are divided into two phases:

  1. Definition (Implementation): Write and compile the code to be used as a function.
  2. Usage (Calling): Use the function name and provide arguments to the function.

Defining a Function#

Creating a New Function Script#

PowerLanguage Editor -> File -> New File -> Function

Return Type determines the type of the function’s return value, divided into Numerical (Numeric), TrueFalse, and String. Function Storage further determines whether the return type is Simple or Series, see Function Input Types for details.

Syntax#

1
2
3
4
[Input: InputName1(InputType), InputName2(InputType), ...;]
// Function's functionality

[FunctionName = ReturnValue;]
Syntax ElementOptionalDescription
InputOptionalDeclares the function’s inputs. Synonym: Inputs
InputNameRequiredThe name of the input
InputTypeRequiredThe type of the input, see Function Input Types
FunctionName = ReturnValueOptionalSpecifies the function’s return value as ReturnValue

Function inputs are different from signal script or indicator script inputs; values cannot be passed from MultiCharts. Function inputs are provided when using (calling) the function.

After writing the function, click the compile button to compile the code. If there are no errors, the function can be used in signal or indicator scripts.

Function Input Types#

Each input type has Numerical (Numeric), String, and TrueFalse versions. Based on the suffix, they are divided into the following 6 categories:

SuffixDescription
SimpleThe value is the same for every bar, no historical values
SeriesThe value may differ between bars, has historical values that can be referenced
(No suffix)Accepts either Simple or Series
RefPassing by Reference, if the input value is changed within the function, the argument’s value also changes
ArrayPasses an Array. M represents the maximum index value for each dimension of the passed array
ArrayRefPassing by Reference for arrays, if the input value is changed within the function, the argument’s value also changes

Numeric#

TypeDescriptionSyntax Example
NumericSimpleSimple NumericalInput: Length(NumericSimple);
NumericSeriesSeries NumericalInput: Price(NumericSeries);
NumericAccepts NumericSimple or NumericSeriesInput: Length(Numeric);
NumericRefNumerical Passing by ReferenceInput: BarCount(NumericRef);
NumericArrayNumerical ArrayInput: Table[X, Y, Z](NumericArray);
NumericArrayRefNumerical Array Passing by ReferenceInput: Table[X, Y, Z](NumericArrayRef);

String#

TypeDescriptionSyntax Example
StringSimpleSimple StringInput: Name(StringSimple);
StringSeriesSeries StringInput: Messages(StringSeries);
StringAccepts StringSimple or StringSeriesInput: Name(String);
StringRefString Passing by ReferenceInput: Message(StringRef);
StringArrayString ArrayInput: MessageTable[X, Y, Z](StringArray);
StringArrayRefString Array Passing by ReferenceInput: CommentsTable[X, Y, Z](StringArrayRef);

TrueFalse#

TypeDescriptionSyntax Example
TrueFalseSimpleSimple TrueFalseInput: Overnight(TrueFalseSimple);
TrueFalseSeriesSeries TrueFalseInput: UpTrend(TrueFalseSeries);
TrueFalseAccepts TrueFalseSimple or TrueFalseSeriesInput: Overnight(TrueFalse);
TrueFalseRefTrueFalse Passing by ReferenceInput: Flag(TrueFalseRef);
TrueFalseArrayTrueFalse ArrayInput: FlagTable[X, Y, Z](TrueFalseArray);
TrueFalseArrayRefTrueFalse Array Passing by ReferenceInput: TrendTable[X, Y, Z](TrueFalseArrayRef);

Using a Function#

Syntax#

1
FunctionName(Argument1, Argument2, ...);
Syntax ElementDescription
FunctionNameThe name of the function to use
ArgumentThe value passed to the input when calling the function, called an argument

Example#

Implementation of the built-in function AverageFC:

File -> Open File -> Function (check the checkbox) -> AverageFC

1
2
3
4
5
Inputs:
  PriceValue(NumericSeries),
  Len(NumericSimple);

AverageFC = SummationFC(PriceValue, Len) / Len;

AverageFC is itself a function implementation that also calls another function SummationFC to complete its functionality. AverageFC declares two inputs: PriceValue of type NumericSeries and Len of type NumericSimple. It then calls the SummationFC function to perform calculations, with PriceValue and Len passed as arguments to SummationFC’s inputs. Finally, AverageFC returns the result of the SummationFC calculation divided by Len.

Passing by Value and Passing by Reference#

Argument#

An argument is the value placed at the function’s input position when calling a function, passed to the function for use.

There are two mechanisms for passing arguments to inputs:

  • Passing by Value
  • Passing by Reference

Passing by Value#

Input types without a Ref suffix use Passing by Value.

Function inputs using Passing by Value cannot have their values changed within the function implementation. Attempting to change the input value within the function will cause compile error: Array, variable or refinput expected.

Example#

The inputs ValueA(NumericSimple) and ValueB(NumericSimple) of the function swapByValFunc are Passing by Value. Attempting to change the values of ValueA and ValueB causes compile error: Array, variable or refinput expected.

Function: swapByValFunc#
1
2
3
4
5
6
7
8
Inputs: ValueA(NumericSimple), ValueB(NumericSimple);
Print("In swapFunc, before swap ValueA: ", ValueA, ", ValueB: ", ValueB);

temp = ValueA;
ValueA = ValueB; // compile error: Array, variable or refinput expected
ValueB = temp; // compile error: Array, variable or refinput expected

Print("In swapFunc, after swap ValueA: ", ValueA, ", ValueB: ", ValueB);

Passing by Reference#

Input types with a Ref suffix use Passing by Reference.

Function inputs using Passing by Reference can have their values changed within the function implementation. If the input value is changed within the function, the argument’s value also changes.

The application of Passing by Reference is to allow a function to have multiple outputs. Since the original function syntax only has one return value, a function can only have one output. When a function needs multiple outputs, Passing by Reference can be used to achieve this.

Example#

The inputs ValueA(NumericRef) and ValueB(NumericRef) of the function swapByRefFunc are Passing by Reference. swapByRefFunc swaps the values of inputs ValueA and ValueB. In the signal testSwapFunc, after calling swapByRefFunc, the values of ValueA and ValueB are swapped.

Function: swapByRefFunc#
1
2
3
4
5
6
7
8
9
Inputs: ValueA(NumericRef), ValueB(NumericRef);
Variables: temp(0);

Print("In swapFunc, before swap ValueA: ", ValueA, ", ValueB: ", ValueB);
// swap ValueA and ValueB
temp = ValueA;
ValueA = ValueB;
ValueB = temp;
Print("In swapFunc, after swap ValueA: ", ValueA, ", ValueB: ", ValueB);
Signal: testSwapFunc#
1
2
3
4
5
6
7
8
9
Variables: ValueA(12), ValueB(34);

// ValueA: 12.00, ValueB: 34.00
Print("Before call swapFunc, ValueA: ", ValueA, ", ValueB: ", ValueB);

swapByRefFunc(ValueA, ValueB);

// ValueA: 34.00, ValueB: 12.00
Print("After call swapFunc, ValueA: ", ValueA, ", ValueB: ", ValueB);

Reference#

https://www.multicharts.com/trading-software/index.php?title=Category:Declaration

https://www.multicharts.com/trading-software/index.php?title=Passing_values_to_and_from_a_function

© 2026 CodeReindeer. All rights reserved.