Tsallaka zuwa abun ciki
Justice Tech School

Functions

Functions can return several values, and types come after names. Variadic parameters use ... and functions are values you can pass around.

Example
package main

import "fmt"

func divmod(a, b int) (int, int) { return a / b, a % b }
func sum(nums ...int) int {
	t := 0
	for _, n := range nums {
		t += n
	}
	return t
}

func main() {
	q, r := divmod(17, 5)
	fmt.Println(q, r, sum(1, 2, 3, 4))
	square := func(x int) int { return x * x }
	fmt.Println(square(6))
}

Result

3 2 10
36

Exercise

Declare a function.

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

Quiz Test

Can a Go function return two values?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.