Function (multiple return values, named return values, variable length arguments, higher-order functions, recursive functions)

We will check how to implement functions with Go. We will implement “multiple return values,” “named return values,” “variable length arguments,” “higher-order functions,” and “recursive functions” and check their operation.

TOC

Basis

Arguments and Return Values

Here is an example of a function that takes an argument of type int and returns a value of type string.

package main

import "fmt"

func func1(x int) string {
	return fmt.Sprint("hello world '", x, "'.")
}

func main() {
	fmt.Println(func1(1234))
	fmt.Println(func1(5678))
}
hello world '1234'.
hello world '5678'.

Multiple Return Values

package main

import "fmt"

func func2(x, y int) (int, int) {
	return x + y, x - y
}

func main() {
	x, y := func2(10, 5)
	fmt.Println(x)
	fmt.Println(y)
}
15
5

Named Return Value

The following example returns a value with a return value named add sub.

package main

import "fmt"

func func3(x, y int) (add, sub int) {
	add = x + y
	sub = x - y
	return
}

func main() {
	x, y := func3(10, 5)
	fmt.Println(x)
	fmt.Println(y)
}
15
5

Variable Length Parameter

Variable-length arguments can be implemented with the notation ....

package main

import "fmt"

func func4(i ...int) int {
	fmt.Println("-----------------")
	fmt.Println(i)
	sum := 0
	for _, v := range i {
		sum += v
	}
	return sum
}

func main() {
	fmt.Println(func4())
	fmt.Println(func4(1))
	fmt.Println(func4(1, 2))
	fmt.Println(func4(1, 2, 3))

	s := []int{1, 2, 3, 4}
	fmt.Println(func4(s...))

}
-----------------
[]
0
-----------------
[1]
1
-----------------
[1 2]
3
-----------------
[1 2 3]
6
-----------------
[1 2 3 4]
10

How to use it one step further

Higher-order functions

Below is an example implementation of a higher-order function (a function that can take other functions as arguments).

package main

import "fmt"

func add(x, y int) int {
	return x + y
}

func sub(x, y int) int {
	return x - y
}

func ope(
	x, y int,
	cb func(int, int) int,
) int {
	return cb(x, y) + 10
}

func main() {
	fmt.Println(ope(10, 5, add))
	fmt.Println(ope(10, 5, sub))
}
25
15

Recursive function

Let’s try to find the factorial with a recursive function.

package main

import "fmt"

func factorial(i int) int {
	if i < 0 {
		return -1
	} else if i == 0 {
		return 1
	} else {
		return i * factorial(i-1)
	}
}

func main() {
	fmt.Println(factorial(-10))
	fmt.Println(factorial(0))
	fmt.Println(factorial(4))
}
-1
1
24

Slice with function as an element

package main

import "fmt"

func main() {
	var tasks = []func(int) string{
		func(i int) string { return fmt.Sprintf("aaa %d", i) },
		func(i int) string { return fmt.Sprintf("bbb %d", i) },
		func(i int) string { return fmt.Sprintf("ccc %d", i) },
	}

	for i, task := range tasks {
		fmt.Println(task(i))
	}
}
aaa 0
bbb 1
ccc 2

Immediate function

package main

import "fmt"

func main() {
	func(m string) {
		fmt.Println(m)
	}("Hello World")
}
Hello World

Reference

Let's share this post !
TOC