Thursday, May 2, 2019

How to take care naughty functions -> "Recursive"

When we play Recursive Function, we need precautions , because it's recursive in nature. There's a big problem if base condition not valid then it repeat infinite times. Which cause panic in go-lang. Today, I use simple example such as factorial 

package main
import (
"fmt"
)
func main() {
i := fact(5)
fmt.Println(i)
}
func fact(n int)int {
if n == 1{
return n
}
return n * fact(n-1) Play 
If you're a detective then you may notice that in func fact return again fact. This line
actually recursive line which repeat over and over, until base condition are true
  

No comments:

Post a Comment

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Lavish parties at farmhouse and farmers pay the rent

    Whenever principals cooked up a storm, many nearby farms suffered for years after the party... Aut city adhonoreum aut aes alienum .. he...

Achieves