Functions
To create a function, we'll add a template from the menu Templates>Language>function echo. This item inserts a code for the echo function, which takes a value and returns it.
function echo(text){
return text;
}
Let's change the function so that it takes the n parameter and displays the values from 0 to n to the console. To do this, we use the count up loop and the system method system.println(). To call a function, you must use the function (args); construct.
Copy the code below and run it. The console must contains the recordings from 0 to 1 and from 0 to 2, for each function call
function count(n){
for(i=0;i<n;i++){
system.println('i='+i);
}
}
system.sleep(2000);
count(2);
system.sleep(2000);
count(3);
When starting the script, note that the output to the console is not done immediately, but with a periodicity of two seconds. Here, for clarity, the system.sleep() method is used, which indicates that the system should wait for the specified number of milliseconds. 2000 milliseconds = 2 seconds.