bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Go/Go Tutorial
Go•Go Tutorial

Go Recursion Functions

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Go Recursion Functions?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ main
3Order

Put the learning moves in the order that makes the concept easiest to apply.

In the following example, testcount() is a function that calls itself.
Go accepts recursion functions.
Recursion Functions

Recursion Functions

Go accepts recursion functions. A function is recursive if it calls itself and reaches a stop condition.

In the following example, testcount() is a function that calls itself. We use the x variable as the data, which increments with 1 ( x + 1 ) every time we recurse. The recursion ends when the x variable equals to 11 ( x == 11 ).

Example

package main
import ("fmt")
func testcount(x int) int {
  if x == 11 {
    return 0
  }
fmt.Println(x)
return testcount(x
+ 1)
}
func main(){
  testcount(1)
}

Recursion is a common mathematical and programming concept. This has the benefit of meaning that you can loop through data to reach a result.

The developer should be careful with recursion functions as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.

In the following example, factorial_recursion() is a function that calls itself. We use the x variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

Example

package main
import ("fmt")
func factorial_recursion(x float64) (y float64) {
  if x > 0 {
    y = x * factorial_recursion(x-1)
  } else {
  y = 1
}
return }
func main() {
  fmt.Println(factorial_recursion(4))
}

To a new developer it can take some time to work out how exactly this works, best way to find out is by testing and modifying it.

Previous

Go Function Returns

Next

Go Struct