Using Nasal functions: Difference between revisions

Jump to navigation Jump to search
m
Line 135: Line 135:
== Returning from functions ==
== Returning from functions ==


In Nasal, functions return implicitly the values of the last expression (e.g. "nil" in empty function bodies), you can also add an explicit "return" statement, for example to leave a function early. In addition, it is possible to return values, too. Whenever a function's signature is empty, because it doesn't take any named arguments, the parentheses are optional, as is the terminating semicolon after a func block:
In Nasal, functions return implicitly the values of the last expression (e.g. "nil" in empty function bodies), and you can also add an explicit "return" statement, for example to leave a function early. In addition, it is possible to return values, too. Whenever a function's signature is empty, because it doesn't take any named arguments, the parentheses are optional. The terminating semicolon after a func block is always optional:


So, semantically, these are all equivalent:
So, semantically, these are all equivalent:
Line 144: Line 144:
var log_message = func() {}
var log_message = func() {}


var log_message = func {return;}
var log_message = func {return;} # the semicolon here is also optional


var log_message = func {nil;} # the semicolon here is also optional
var log_message = func {nil;} # and here too, of course


var log_message = func {};  
var log_message = func {};  


var log_message = func return;
var log_message = func return; # look, no braces! the function ends at the semicolon


var log_message = func nil;
var log_message = func nil;
Line 156: Line 156:
var log_message = func() nil;
var log_message = func() nil;


var log_message = func;
var log_message = func; # emptiness is equivalent to "nil"
</syntaxhighlight>
</syntaxhighlight>


395

edits

Navigation menu