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

خیال قیام پزر

 اکثر دل میرا ڈھڑتا ہے، جب کبھی  بادل ٹھہر جاتے ہیں، میرے آنگن کے  چاروں ظرف اک چاند ہے اُس کا، خیال قیام پزر ہوتا دل میں میرے ، کوئی گ...

Achieves