Creating functions
Most programs will need to execute code from different NAV objects. This code is contained in functions. This recipe will show you how to create a function and explain in more detail what functions are.
How to do it...
To start, let's create a new codeunit from Object Designer.
Add a function called
CountToN
that takes an integer parametern
.Now add the following global variables:
Name
Type
i
Integer
Write the following code in the function:
FOR i := 1 TO n DO MESSAGE('%1', i);
Write the following code in the
OnRun
trigger of the codeunit:CountToN(3);
It's time to save and close the codeunit.
On execution of the codeunit, you should see a window similar to the following screenshot:
How it works...
By creating a function, we can reference multiple lines of code using one easy-to-understand name. Our function is called CountToN
, and it takes an integer n
as a parameter. This function will display a message box for every number ranging between one and the number that is passed...