Using Nasal functions: Difference between revisions

Jump to navigation Jump to search
Line 46: Line 46:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
var hello = func(who) {
var hello = func( name ) {
     print("Hello ", who);
     print("Hello ", name);
}
}
</syntaxhighlight>
</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.


By using so called "function arguments" (see below) it is possible to parametrize a function (using variables) so that it may use data that is specific to each call/invocation.
By using so called "function arguments" (see below) it is possible to parametrize a function (using variables) so that it may use data that is specific to each call/invocation.
As you can see, the '''hello'' function takes a single argument named '''name''' and then runs the '''print''' function which prints two words: 1) Hello and 2) the name specified when calling hello().


As previously shown, Nasal functions are implemented using the func keyword, The following snippet of code defines a new function named "log_message" with an empty function body (the curly braces).
As previously shown, Nasal functions are implemented using the func keyword, The following snippet of code defines a new function named "log_message" with an empty function body (the curly braces).

Navigation menu