Functions Functions are reusable blocks of code that can accept parameters and return values.
Function Declaration Functions are declared using the func keyword followed by a name, parameters, return types, and body.
Basic Syntax 1
2
3
func name () {
// function body
}
Example 1
2
3
4
5
func greet () {
printf ( "Hello, world!\n" )
}
greet () // call the function
Parameters Functions can accept zero or more parameters, each with a name and type.
Single Parameter 1
2
3
4
5
func greet ( name string ) {
printf ( "Hello, %s!\n" , name )
}
greet ( "John" )
Multiple Parameters 1
2
3
4
5
func add ( a number , b number ) {
printf ( "Sum: %g\n" , a + b )
}
add ( 5 , 3 )
Return Values Functions can return values using the return statement.
Single Return Value 1
2
3
4
5
func double ( x number ) number {
return x * 2
}
var result number = double ( 5 ) // result is 10
Multiple Return Values Functions can return multiple values.
1
2
3
4
5
6
7
func getCoordinates () number , string {
return 42 , "hello"
}
var x number
var label string
x , label = getCoordinates ()
Parenthesized Return Types Multiple return types can optionally be wrapped in parentheses for clarity.
1
2
3
func getCoordinates () ( number , string ) {
return 42 , "hello"
}
Both styles are equivalent.
Return Statement The return statement exits the function and optionally returns values.
Returning Values 1
2
3
4
5
6
7
func getMax ( a number , b number ) number {
if a > b {
return a
}
return b
}
Early Return You can use return to exit a function early:
1
2
3
4
5
6
7
8
9
func checkValue ( x number ) {
if x < 0 {
printf ( "Negative value\n" )
return
}
printf ( "Positive value: %g\n" , x )
}
Returning Null Functions can return null as a value:
1
2
3
4
5
6
7
func maybeGetValue ( flag bool ) number {
if flag {
return 42
}
return null
}
Function Calls Functions are called by their name followed by arguments in parentheses.
Basic Call 1
2
3
4
5
func sayHello () {
printf ( "Hello!\n" )
}
sayHello ()
With Arguments 1
2
3
4
5
func multiply ( a number , b number ) number {
return a * b
}
var result number = multiply ( 4 , 5 ) // 20
Using Spread Operator When a function returns multiple values, use the spread operator to pass them as arguments to another function:
1
2
3
4
5
6
7
8
9
func getTwo () number , number {
return 10 , 20
}
func addTwo ( a number , b number ) number {
return a + b
}
var sum number = addTwo ( ... getTwo ()) // sum is 30
You can also use it to pass array elements as arguments:
1
2
3
4
5
6
func printThree ( a number , b number , c number ) {
printf ( "%g, %g, %g\n" , a , b , c )
}
var numbers [] number = [ 1 , 2 , 3 ]
printThree ( ... numbers ) // prints "1, 2, 3"
Function Examples Simple Function 1
2
3
4
5
func test () number {
return 1
}
printf ( "test(): %g\n" , test ())
Function with Parameter 1
2
3
4
5
func testWithNumber ( count number ) number {
return count
}
printf ( "testWithNumber(): %g\n" , testWithNumber ( 2 ))
Multiple Return Values 1
2
3
4
5
func testMultiple () number , string {
return 1 , "hello"
}
printf ( "testMultiple(): %g, %s\n" , ... testMultiple ())
Complex Function 1
2
3
4
5
func testComplex ( count number , name string ) ( number , string ) {
return count , null
}
printf ( "testComplex(): %g, %s\n" , ... testComplex ( 1 , "hello" ))
Scope Functions have their own scope. Variables declared inside a function are local to that function.
1
2
3
4
5
6
7
8
9
10
11
var global string = "global"
func myFunction () {
var local string = "local"
printf ( "%s\n" , global ) // can access global
printf ( "%s\n" , local ) // can access local
}
myFunction ()
printf ( "%s\n" , global ) // works
printf ( "%s\n" , local ) // Error: local not defined
Recursion Functions can call themselves recursively.
1
2
3
4
5
6
7
8
9
func factorial ( n number ) number {
if n <= 1 {
return 1
}
return n * factorial ( n - 1 )
}
printf ( "5! = %g\n" , factorial ( 5 )) // 120
Improve this page