Recursion

The Go+ programming language supports recursion. That is, it allows a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go on to become an infinite loop. Recursive functions are very useful to solve many mathematical problems such as calculating factorial of a number, generating a Fibonacci series, etc.

This example calculates the factorial of a given number using a recursive function

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

This Example shows how to generate a Fibonacci series of a given number using a recursive function

func fibonaci(i int) (ret int) { if i == 0 { return 0 } if i == 1 { return 1 } return fibonaci(i-1) + fibonaci(i-2) } println "Factorial of 15 is", factorial(15) for i := 0; i < 10; i++ { println fibonaci(i) }

Next example: Variadic Parameters