swift - Calling a function inside itself -
i have function, , in cases want used 2 times in row, there way call function inside
something like, function lot longer , being abel save lot of time
func thefunc() { count++ if count < 4 { thfunc() } }
that's called recursion, , it's legal:
var count = 0 func thefunc() { print(count) count += 1 if count < 4 { thefunc() } } thefunc() // 0 1 2 3
the trick not recurse deeply, risk running out of resources, , don't forget put sort of "stopper" (such if count < 4
), lest recurse forever, resulting in (oh irony) stack overflow.
[extra experts: there languages, such lisp, optimized recursion, , recursion preferred looping! swift not 1 of those.]
Comments
Post a Comment