Updated 16 Nov 2026: rewritten. The original called pointer passing “call by reference” and its swap example returned a pointless value; this version states the by-value rule correctly and adds the error idiom and closures.
A function is a named block of code you can call from elsewhere. This post covers declaring one, returning several results, accepting a varying number of arguments, and using functions as values.
Declaring and calling #
func add(x int, y int) int {
return x + y
}The parts are the func keyword, the name, the parameters with their types, and the return type after the parentheses. When consecutive parameters share a type, you can write it once:
func add(x, y int) int {
return x + y
}Call it with arguments:
result := add(3, 5)
fmt.Println(result) // 8Parameters are the names in the declaration (x, y). Arguments are the values you pass in the call (3, 5).
Names follow the same visibility rule as everything else in Go: a function whose name starts with a capital letter (Add) is exported and can be called from other packages, and one that starts lowercase (add) is private to its package. Go has no function overloading, so each name in a package refers to exactly one function.
Multiple return values #
A function can return more than one value. This is what makes Go’s error handling work:
import "errors"
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 4)
fmt.Println(result, err) // 2.5 <nil>
_, err = divide(1, 0)
fmt.Println(err) // division by zero
}By convention the last return value is an error, and nil means success. The caller checks err straight after the call. Use _ when you don’t want one of the values.
You can name the results, which documents them and lets you use a bare return:
func minMax(a, b int) (lo, hi int) {
if a < b {
return a, b
}
return b, a
}Named results help in short functions. In long ones, a bare return makes it hard to see what’s being returned, so spell the values out.
Variadic functions #
A final parameter written with ... accepts any number of arguments. Inside the function, it’s a slice:
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
fmt.Println(sum()) // 0
fmt.Println(sum(10, 20)) // 30
fmt.Println(sum(1, 2, 3, 4)) // 10If you already have a slice, pass it with ... after it: sum(values...). fmt.Println is a variadic function, which is why it takes any number of arguments.
Everything is passed by value #
When you call a function, Go copies each argument into the parameter. The function works on the copy, so changing it doesn’t affect the caller’s variable:
func double(n int) {
n = n * 2 // changes the copy only
}
x := 5
double(x)
fmt.Println(x) // 5To let a function change the caller’s variable, pass a pointer. The pointer itself is still copied, but the copy points at the same variable:
func swap(a, b *int) {
*a, *b = *b, *a
}
p, q := 10, 20
swap(&p, &q)
fmt.Println(p, q) // 20 10People sometimes call this “pass by reference”, but Go has no references in that sense. It always passes a copy, and a pointer is a value that happens to be an address. The pointers post goes into this.
Slices, maps and channels behave as if passed by reference, because the value that’s copied is a small header that points at shared data. That’s why a function can change a map’s contents without a pointer.
Functions are values #
A function can be assigned to a variable, passed as an argument and returned from another function. A function without a name, written inline, is an anonymous function:
add := func(a, b int) int {
return a + b
}
fmt.Println(add(3, 4)) // 7A function that takes another function as a parameter is called a higher-order function:
func apply(fn func(int, int) int, a, b int) int {
return fn(a, b)
}
fmt.Println(apply(add, 4, 7)) // 11Closures #
An anonymous function can use variables from the scope around it, and it keeps them alive for as long as the function exists. That combination is a closure:
func counter() func() int {
n := 0
return func() int {
n++
return n
}
}
func main() {
next := counter()
fmt.Println(next()) // 1
fmt.Println(next()) // 2
fmt.Println(next()) // 3
}Each call to counter() creates its own n, and the returned function updates it. Closures are how Go code builds things like handlers, iterators and options.
Recursion #
A function can call itself. It needs a base case to stop:
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
fmt.Println(factorial(5)) // 120defer #
defer schedules a call to run when the surrounding function returns. It’s how you guarantee cleanup, such as defer file.Close(). Deferred calls run in last-in, first-out order, as covered in the control flow post.
Next: pointers.