What is recursion and what are the advantages and disadvantages of recursion?

What is recursion and what are the advantages or disadvantages of recursion?
Recursion: A function is recursive if it can call itself internally. Simple understanding : letter
The number calls itself internally , this function is a recursive function
Advantages: clear structure, strong readability
Disadvantages: low efficiency, the call stack may overflow, in fact, each function call will allocate space in the memory stack, and each process
The capacity of the stack is limited. When there are too many levels of calls, the capacity of the stack will be exceeded, resulting in stack overflow.
function fn(x, y) {
    x += y
    y ++;
    if (y > 100) {
        console.log(x);
        return x
    }
    else {
        fn(x, y)    // fn()函数内部重复调用自身,在y<100时,会一直将x,y当参数执行fn()
    }
}


Guess you like

Origin blog.csdn.net/qq_59020839/article/details/127354001