Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:17:26+00:00 2026-06-04T04:17:26+00:00

Every textbook I’ve ever seen on recursive functions uses the factorials as an example,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:17:27+00:00Added an answer on June 4, 2026 at 4:17 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every example I can find is in C++, but I'm trying to keep my
By textbook based i mean the author asks the reader questions/mini projects after every
This is a moot question as I'm not on this project any more, but
I am looking for an example in which shows dispose pattern of .net with
Every now and then my selenium tests randomly fail with an element X not
Every once in a while, but especially prevalent on Facebook, Opera Mini will exhibit
Besides textbook examples -- in the real world -- does it ever make sense
Every software I have seen, contain copyright statement at the beginning of a file
Every example I can find either goes through creating a table without a gui
Every time I launch the Android emulator under Windows, it asks me for which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.