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

Prince and time chessboard pieces moved more than once

 Prince! .... Had nothing to do perhaps, time again whispered with an opportunity to touch the sky once or more then once.  Casear dispatche...

Achieves