-
Notifications
You must be signed in to change notification settings - Fork 4
Functions
Functions in Volant are expressions and can be used as other ordinary values. Function expressions have syntax,
func (arg1: type1, arg2: type2) returnType {
// do stuff
}
Functions can be declared just like ordinary values. The type signature of functions has syntax func (type1, type2, type3) returnType
.
function: func(type1, type2) returnType;
function = func (arg1: type1, arg2: type2) returnType {
// do stuff
};
// or use implicit declaration
function := func (arg1: type1, arg2: type2) returnType {
// do stuff
};
The return type can be omitted from the function expression and type if the function does not return anything. For example,
// create a function that does not return anything
function: func (typ1, type2) = func (arg1: type1, arg2: type2) {
// do stuff
};
Volant provides one more way of declaring functions,
func name(arg1: type1, arg2: type2) returnType {
//do stuff
}
Functions declared with this syntax are constants and cannot be re-initialized. There is a very specific reason for the two syntaxes to exist separately, talking about which is beyond the scope of this page. It is preferred to the second syntax whenever possible.
A function can return a value using return
statements with syntax return anExpression;
. The function execution is stopped when it returns a value. return
can also be used without an expression to stop the execution of the function in functions that do not return anything.
Functions are invoked with syntax function(arg1, arg2, arg3)
which is an expression that resolves to the value returned by the function.
Volant allows nesting functions along with support for closures.