Very common programming languages support recursive functions, and Go is no exception. The so-called recursive function refers to the function that calls the function itself within the function. From the perspective of mathematical problem-solving ideas, recursive means splitting a big problem into multiple small problems and then breaking them one by one. In the actual development process, recursive functions can solve many mathematical problems, such as calculating a given number factorial, generating a Fibon series, etc.
To constitute recursion, the following conditions are required:
- A problem can be split into multiple subproblems;
- The original problem before splitting and the sub-problem after splitting are different except for the data size, but the idea of dealing with the problem is the same;
- The call itself cannot be unlimited, and the sub-problem needs to have the conditions to exit the recursive state.
Note: When writing recursive functions, you must have a termination condition, otherwise it will be called infinitely until the memory overflows.
The following is a few examples to demonstrate the use of recursive functions.
Fibonacci sequence
Let’s take the classic example of recursive functions - Fibonacci sequences as an example to demonstrate how to print Fibonacci sequences through recursive functions written in Go language.
The form of the sequence is as follows:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, …
The specific code for implementing the Fibonacci sequence using Go recursive function is as follows:
package main import "fmt" func main() { result := 0 for i := 1; i <= 10; i++ { result = fibonacci(i) ("fibonacci(%d) is: %d\n", i, result) } } func fibonacci(n int) (res int) { if n <= 2 { res = 1 } else { res = fibonacci(n-1) + fibonacci(n-2) } return }
The output result is:
fibonacci(1) is: 1
fibonacci(2) is: 1
fibonacci(3) is: 2
fibonacci(4) is: 3
fibonacci(5) is: 5
fibonacci(6) is: 8
fibonacci(7) is: 13
fibonacci(8) is: 21
fibonacci(9) is: 34
fibonacci(10) is: 55
Number Multiplication
The factorial of a positive integer is the product of all positive integers less than or equal to that number, and the factorial of 0 is 1, and the factorial of the natural number n is written as n!, "Kiston Kaman" invented the operator n! in 1808.
For example, n!=1×2×3×…×n, factorials can also be defined recursively: 0!=1, n!=(n-1)!×n.
Use a recursive function to calculate the factorial of a given number, the example code is as follows:
package main import "fmt" func Factorial(n uint64) (result uint64) { if n > 0 { result = n * Factorial(n-1) return result } return 1 } func main() { var i int = 10 ("The factorial of %d is %d\n", i, Factorial(uint64(i))) }
The output result is:
The factorial of 10 is 3628800
Multiple functions make up recursion
Recursive functions that are called mutually can also be used in Go language. Multiple functions call each other to form a closed loop. Because of the special nature of the Go language compiler, the declaration order of these functions can be arbitrary. The following simple example shows the mutual calls between functions odd and even:
package main import ( "fmt" ) func main() { ("%d is even: is %t\n", 16, even(16)) // 16 is even: is true ("%d is odd: is %t\n", 17, odd(17)) // 17 is odd: is true ("%d is odd: is %t\n", 18, odd(18)) // 18 is odd: is false } func even(nr int) bool { if nr == 0 { return true } return odd(RevSign(nr) - 1) } func odd(nr int) bool { if nr == 0 { return false } return even(RevSign(nr) - 1) } func RevSign(nr int) int { if nr < 0 { return -nr } return nr }
The operation effect is as follows:
16 is even: is true
17 is odd: is true
18 is odd: is false
This is the introduction to this article about the specific implementation of Go recursive functions. For more related content of Go recursive functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!