Function Declaration
func multiplyAdd(Float x, y, Int z) -> Float {
return x * y + Float(z)
}
Function declarations start with the keyword func, as in Swift.
Easier parsing due to clear distinction between function declaration vs. variable declaration, avoiding the most vexing parse.
Using the trailing return type syntax -> ReturnType, but void functions (AKA “procedures”) are written without trailing :-> Void
func print(String line) { ... }
Function parameters are given as TypeName parameterName, multiple function parameters of the (exact) same type can be combined:
func multiply(Int x, y) -> Int // x and y are Int
Misc
- Lambdas as in C++
[](Int i) -> Float { i * 3.1415926 } constMember Functionsclass MyArrayOfInt { const func size() -> Int { ... } }constexpr,constevalconstexpr multiply(Int x, y) -> Int { return x * y } consteval multiply(Int x, y) -> Int { return x * y }
Function Pointers
Trying to maintain consistency between declarations of functions, function pointers, functors and lambdas.
Examples:
func(Int, Int -> Int)* pointerToFunctionOfIntAndIntToIntfunc(Int)* pointerToFunctionOfIntfunc(Int, Int -> Int)& referenceToFunctionOfIntAndIntToInt// Can’t be zerofunc(Int)& referenceToFunctionOfInt
Function Parameters
By default, function arguments are passed as const reference. Only small types (with a size of up to 16 bytes) are passed as const value.
For details, see Function Parameters.