Functions
Defining functions
Functions are declared with the function keyword:
Parameters
Parameters are listed with their types:
Multiple parameters are separated by commas. Each parameter must have a type annotation.
Return values
To return a value, specify the return type after the parameter list and use return:
Functions without a return type implicitly return nil.
A bare return (with no value) is also valid and returns nil:
function maybeReturn(bool condition) {
if (condition) {
return
}
console.println("condition was false")
}
Calling functions
Optional arguments
Parameters can have default values. Optional parameters must come after all required parameters:
function repeat(string text, int times = 1) string {
var string result = ""
for i in range(times) {
result = result + text
}
return result
}
When calling, you can omit optional arguments to use their defaults:
Keyword arguments
You can pass arguments by name using name=value syntax:
function greet(string name, string greeting) {
console.println(f"{greeting}, {name}!")
}
function main() {
greet("World", "Hello") // positional
greet(greeting="Hi", name="CColon") // keyword
greet("Bob", greeting="Hey") // mixed
}
Positional arguments must come before keyword arguments. You cannot provide the same argument both positionally and by name.
The main function
When running a .ccl file, CColon looks for a function named main and calls it automatically. This is the entry point for every program:
Functions defined outside of main are available globally and can be called from main or from each other.
Recursion
Functions can call themselves:
There is no explicit recursion depth limit beyond available stack space.
Functions as values
Functions are stored as global values. You can pass them around and call them indirectly: