Functions in X++: Syntax, Usage, and Examples
Table of Content:
In Microsoft Dynamics AX, the X++ language provides more than 100 system functions that are not part of any class. The two types of functions are as follows:
-
Run time – functions that are executed during run time.
-
Compile time – functions that are executed during compile time.
Run Time Functions
Run time functions used for data type conversions, mathematical operations, and so on. Some common run time functions are as follows:
-
str2Int – creates an int value from a str value.
-
abs – creates a positive real value from a real value that is either positive or negative.
-
conFind – retrieves the location of an element in a container.
Compile Time Functions
Compile time functions are executed early in the compile of X++ code. Most of these functions retrieve metadata about items that are in the Application Object Tree (AOT). Some common compile time functions are as follows:
-
classNum – retrieves the ID of a class.
-
classStr – verifies during compile time that a class by that name exists. This is better than discovering the error later during run time.
-
evalBuf – evaluates the input string of X++ code, and then returns the results as a string.
-
literalStr – retrieves a label ID when given the string representation of a label, such as the string "@SYS12345". For example, myLabel.exists(literalStr("@SYS12345"));.
Function Example
In X++ programming language, a function is a block of code that performs a specific task or computation. Functions are defined to encapsulate a set of instructions that can be reused throughout the code. They help in modularizing the code and improving its readability, maintainability, and reusability.
In X++, functions are declared using the "static" keyword and are part of classes. The basic syntax of a function in X++ is as follows:
public static returnType functionName(parameters) { // Function body // Code to perform the task or computation return returnValue; // If the function has a return type }
Here's a breakdown of the components:
-
public: It defines the access level of the function. In this case, it's public, meaning the function can be accessed from other classes.
-
static: It indicates that the function is associated with the class itself rather than with an instance of the class. It means you can call the function without creating an object of the class.
-
returnType: It specifies the data type of the value that the function will return. If the function doesn't return anything, you can use the "void" keyword.
-
functionName: It is the name given to the function. It follows the same naming rules as other programming languages (e.g., no spaces, meaningful names, etc.).
-
parameters: These are optional, but if the function requires input values to perform its task, you can define parameters inside parentheses. Multiple parameters can be separated by commas.
-
Function body: It consists of the code that executes the desired task or computation when the function is called.
-
return: If the function has a return type specified, you need to use the "return" statement to send a value back to the calling code.
Here's a simple example of a function in X++:
class MathOperations { public static int addNumbers(int num1, int num2) { int result = num1 + num2; return result; } }
In this example, we have a class called MathOperations
with a static function named addNumbers
. This function takes two integer parameters num1
and num2
and returns their sum.
You can call this function from other parts of the code like this:
int sum = MathOperations::addNumbers(5, 7);
In this case, sum
will hold the value 12 (result of 5 + 7).