Using Nasal functions: Difference between revisions

Jump to navigation Jump to search
m
Line 6: Line 6:
=== What is a function ? ===
=== What is a function ? ===


A "function" is a piece of code that can be easily used repeatedly (without repeating the same code over and over again), this is achieved by associating a symbolic name with the piece of code, such as "print", "show" or "get" for example. Imagine having a piece of code that multiplies two vectors of numbers with each other. Now, if you wanted to use this code in different places, you would have to repeat (copy/paste) the same code over and over again. This is where we instead assign a symbolic name to the code and wrap it in curly braces.
A "function" is a piece of code that can be easily used repeatedly (without repeating the same code over and over again using your editor's copy&paste function), this is achieved by associating a symbolic name with the piece of code, such as "print", "show" or "get", setprop, getprop for example.
 
In its most basic form, a function does not even take any arguments. Let's imagine a function named '''hello''', functions are typically called by appending parentheses to the function name:
 
<syntaxhighlight lang="php">
hello()
</syntaxhighlight>
 
As a single statement, you'd want to terminate the instruction using a semicolon:
 
<syntaxhighlight lang="php">
hello();
</syntaxhighlight>
 
However, you do not always need to terminate a function call, especially not embedded calls, imagine this:
 
<syntaxhighlight lang="php">
hello( hello() );
</syntaxhighlight>
 
This will first call the inner hello() function, and afterwards the outer function. Note how only the outer function call has parentheses.
 
You may be wondering what's happening here by calling the hello() function this way, in a nested fashion. The truth is, functions don't always have empty parentheses - the parentheses are there to pass function arguments (parameters) to the function:
 
<syntaxhighlight lang="php">
hello( "FlightGear" );
</syntaxhighlight>
 
This passes a single function argument to the hello function.
 
Imagine having a piece of code that multiplies two vectors of numbers with each other. Now, if you wanted to use this code in different places, you would have to repeat (copy/paste) the same code over and over again. This is where we instead assign a symbolic name to the code we want to re-use and wrap it in curly braces, the implementation of the hello function (its function body) may look like this:
 
<syntaxhighlight lang="php">
var hello = func(who) {
print("Hello ", who);
}
</syntaxhighlight>
 


Whenever this symbolic name is then used in the program, the program will "jump" to the definition of the function and start running it, once the called function has completed it will automatically return to the instruction following the call.
Whenever this symbolic name is then used in the program, the program will "jump" to the definition of the function and start running it, once the called function has completed it will automatically return to the instruction following the call.

Navigation menu