Wednesday, August 27, 2014

Local functions

Local functions

Local functions are the functions that placed in another method. In AX, we can add local functions inside to the job or class methods but we can’t add local functions inside the local function, it is restricted. Local functions are used to separate the business logics. The best practice is to avoid to use local functions, instead of this we can create private methods.

Create local function:

Local function should be placed end of the declaration part.
Note: we can’t declare the variable next to the local functions.

Scope of variables:

Local function can access the method variables. For example, Localfunction1 can access the Hari_LocalFunctions method i variable. But Hari_LocalFunctions method can’t access the local function variables (Localfunction1 function j variable)

Calling local functions:

We can call the local function as usual method of calling method or function like Localfunction1().
We can call the local function from top level declared local function. For example, we can call the localFunction1 local function from localFunction2 local function, but we can’t call the localFunction2 from localFunction1 local function. Because the localFunction2 is declared next to the localFunction1 local function.
Local function is also supporting recursive function calling method. Note: We should be careful when we using recursive function.

Example code:

I have created the job for your better understanding.
static void Hari_LocalFunctions(Args _args)
    int i = 1;
    void localFunction1()
        int j = 2;
        info(strFmt('Method 1 - %1', i));
    void localFunction2()
        info(strFmt('Method 2 - %1', i));

No comments :

Post a Comment