Functions

Functions

Functions are first-class citizens in Go.

Basic Syntax

func functionName(param1 type1, param2 type2) returnType {
    return value
}

Example

func add(a int, b int) int {
    return a + b
}

result := add(5, 3) // 8

Multiple Returns

Go functions can return multiple values:

func divide(a, b int) (int, int) {
    return a / b, a % b
}

quotient, remainder := divide(10, 3)

Named Returns

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // returns x, y
}
Visualizer
function
add()
5
3
8

Interactive Visualization

go
Loading...
Output
Run execution to see output...