Arguments and return values
Return a value from a function with defaults
Choose a useful value when the caller omits the trailing input.
func greet(name, salutation = 'Hello'):
return '{salutation}, {name}!'
print(greet('Ada'))
Hello, Ada!
The call passes the name and uses the default salutation. The returned value is text.
Parameters and defaults
Arguments fill parameters by position. A trailing default supplies a value when that argument is omitted.
Variadic arguments
A final '...rest' parameter gathers the remaining arguments.
Return values and recursion
Use 'return value' to send a result back to the caller. A named function can call itself by its own name.
Call arguments are positional. Defaults are evaluated only for omitted arguments, and can refer to earlier parameters. Missing required arguments and extra arguments on fixed-arity functions raise errors. A return takes zero or one value. Use an array or dictionary for multiple results. Named declarations form immutable lexical bindings. Recursion works through the declared function name. Anonymous functions use func(parameters): with an indented suite and can be assigned or passed to other functions. The word self is an ordinary identifier with receiver behavior when it is the first required parameter. Prefer factories that return a configured dictionary of values and methods, or a closure with private captured state.