Using Nasal functions: Difference between revisions

Jump to navigation Jump to search
no edit summary
m (lang="php" -> lang="nasal" (I assume Gijs will run his bot later - it looks pretty good to me!))
No edit summary
Line 1: Line 1:
{{Template:Nasal Navigation}}
{{Template:Nasal Navigation}}


'''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 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.
'''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 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 - here's a simple function that squares a number:
 
<syntaxhighlight lang="nasal">
var square = func(number) {
return number*number;
}
</syntaxhighlight>
 
This can be interpreted as:
* create a new variable named 'square'
* assign a function to it
* make the function accept a single argument (parameter), named '''number''' (the part in parentheses)
* begin/open the function body (the opening curly brace)
* return the result of number*number to the caller
* close the function body (the closing curly brace)


So, to put things in layman's terms, imagine your function like a recipe in a cooking book - instead of always having to write down the recipe, you just open the cooking book, "jump" to the corresponding page, "execute" the recipee and then "return" to where you left off earlier.
So, to put things in layman's terms, imagine your function like a recipe in a cooking book - instead of always having to write down the recipe, you just open the cooking book, "jump" to the corresponding page, "execute" the recipee and then "return" to where you left off earlier.

Navigation menu