Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add help for recursion #44

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 206-Recursion/recursion.gop
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
//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)
}