# Go 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)
}

Result:

``` 12345678910```
Try it Yourself »

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))
}

Result:

``` 24 ```
Try it Yourself »

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.

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.