Skip to content

Commit 5a57907

Browse files
committed
WIP recursion
1 parent accbea5 commit 5a57907

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ With that in mind, Golang is a language with fewer abstractions when comparing t
3434
I believe to get a good understanding, you can follow this order:
3535

3636
- [Concepts](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/concepts)
37+
- [Recursion](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/recursion) --- WIP

data-structures-algorithms-go

-2.08 MB
Binary file not shown.

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import "github.com/mfbmina/data-structures-algorithms-go/concepts"
4+
import "github.com/mfbmina/data-structures-algorithms-go/recursion"
45

56
func main() {
67
// conceptsExamples()
8+
recursionExamples()
79
}
810

911
func conceptsExamples() {
@@ -16,3 +18,7 @@ func conceptsExamples() {
1618
concepts.Structs()
1719
concepts.Functions()
1820
}
21+
22+
func recursionExamples() {
23+
recursion.Basic(10)
24+
}

recursion/basic.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package recursion
2+
3+
import "fmt"
4+
5+
// In short, recursion is when a function call itself.
6+
//
7+
// When using recursion is important to always have a stop condition
8+
// because if there isn't one, it will consume all your memory.
9+
func Basic(n int64) {
10+
fmt.Println("Calculating fatorial for", n)
11+
resp := fat(n)
12+
fmt.Println("The response is", resp)
13+
}
14+
15+
func fat(n int64) int64{
16+
// stop condition
17+
if n == 1 {
18+
return 1
19+
}
20+
21+
// calling itself again!
22+
return n * fat(n - 1)
23+
}

0 commit comments

Comments
 (0)