简体   繁体   中英

More to Recursion than Just a Function Calling Itself

Every textbook I've ever seen on recursive functions uses the factorials as an example, which is helpful but not totally illuminating.

For programming purposes, can a recursive function take a function as its base case, can it include other function calls within its body, or can it execute differently at different levels of recursion?

And if it does these any of these things, is it still a 'recursive function' or is it now something else?

The definition of a recursive function is simply "a function that calls itself". So if you have a function that calls itself, it is a recursive function.

Anything else simply depends on the capabilities of the language you're working with.

This is a fairly straightforward example of a recursive function that simply outputs all of the values in a collection, an array represented as a stack, to the browser console as it pops them off of the stack using the .pop() method.

The totalSize value doesn't change throughout the entire call stack so that it can be used to measure the halfway point by dividing the current stack size by the original size.

To answer the question of can it behave differently at different levels in the recursion, the answer is yes:

// a simple array of 10 items
var coll = [1,2,3,4,5,6,7,8,9,10];

// recursive function that calls itself. It behaves slightly different at
 // the halfway point
function test(totalSize, col) {
    if(col == undefined || col.length == 0) {
        return 0;

     } else {
        if(col.length / totalSize < .5) {
            console.log("Past 1/2 way point!");
        }
        console.log("total = " + totalSize);
        console.log("col.pop() = " + col.pop());
        return test(totalSize, col);
    }
}

// make a call to the function with the total size and the collection
test(coll.length, coll);

Additionally, you also asked if it were possible to call other functions, this is also possible. In the example below, a function is used to return the result of the base case, and a function is used to abstract the halfway point behavior:

// a simple array of 10 items
var coll = [1,2,3,4,5,6,7,8,9,10];

// recursive function that calls itself. It behaves slightly different at
 // the halfway point
function test(totalSize, col) {
    if(col == undefined || col.length == 0) {
        return handleBaseCase(totalSize, col);

     } else {
        // handle if it's at 1/2 way point
        handleHalfwayPoint(totalSize, col);  

        console.log("tital = " + totalSize);
        console.log("col.pop() = " + col.pop());
        return test(totalSize, col);
    }
}

function handleHalfwayPoint(totalSize, collection) {
    if(collection.length / totalSize < .5) {
        console.log("Past 1/2 way point!");
    }
}

// instead of returning 0, return "Done" and also print to the log
function handleBaseCase(totalSize, collection) {
    console.info("Done!");
    return "Done!";
}

// make a call to the function with the total size and the collection
test(coll.length, coll);

While these particular examples don't solve any real-world problem, it demonstrates how the concept of calling a function inside another function could expand to handle other use-cases. The examples in your textbook are simply designed to teach you the basics of recursion and help arm you with the tools necessary to take on more complex, real-world problems in the future.

Since this functional language is JavaScript, the barriers to running them are low. You can try these examples by running the code in the Chrome Developer Console, or you could run them in a small test HTML file. Good luck!

A recursive function is a function that calls itself one or more times in its body. Functions can also be mutually recursive, where one function is defined in terms of the second, or vice-versa.

I've been programming for over 20 years without the need for recursion. What made me really understand the need, as well as the beauty, of recursive calls was the Scheme language, and books such as "The little Schemer".

Not all programming languages support recursion at the same level. Scheme is one of those who do it very well. Python much less so. So if you want to dive into recursion, check the abilities of your programming language.

As other answers have said, a recursive function is a function that calls itself. There are two types as explained here :

  1. Direct recursion: in which the function calls itself.
  2. Indirect recursion: when a function is called not by itself but by another function that it called (either directly or indirectly).

I see the math tag in your question. Recursion is related to math induction. If you prove that it can solve the base case and then you prove that if any one statement in the infinite sequence of statements is true, then so is the next one, you prove that it will solve the problem in any case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM