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 7577941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:18:44+00:00 2026-05-30T17:18:44+00:00

A very common beginner mistake when writing recursive functions is to accidentally fire off

  • 0

A very common beginner mistake when writing recursive functions is to accidentally fire off completely redundant recursive calls from the same function. For example, consider this recursive function that finds the maximum value in a binary tree (not a binary search tree):

int BinaryTreeMax(Tree* root) {
    if (root == null) return INT_MIN;

    int maxValue = root->value;
    if (maxValue < BinaryTreeMax(root->left))
        maxValue = BinaryTreeMax(root->left);   // (1)
    if (maxValue < BinaryTreeMax(root->right))
        maxValue = BinaryTreeMax(root->right);  // (2)

    return maxValue;
}

Notice that this program potentially makes two completely redundant recursive calls to BinaryTreeMax in lines (1) and (2). We could rewrite this code so that there’s no need for these extra calls by simply caching the value from before:

int BinaryTreeMax(Tree* root) {
    if (root == null) return INT_MIN;

    int maxValue = root->value;
    int leftValue = BinaryTreeMax(root->left);
    int rightValue = BinaryTreeMax(root->right);

    if (maxValue < leftValue)
        maxValue = leftValue;
    if (maxValue < rightValue)
        maxValue = rightValue;

    return maxValue;
}

Now, we always make exactly two recursive calls.

My question is whether there is a tool that does either a static or dynamic analysis of a program (in whatever language you’d like; I’m not too picky!) that can detect whether a program is making completely unnecessary recursive calls. By “completely unnecessary” I mean that

  1. The recursive call has been made before,
  2. by the same invocation of the recursive function (or one of its descendants), and
  3. the call itself has no observable side-effects.

This is something that can usually be determined by hand, but I think it would be great if there were some tool that could flag things like this automatically as a way of helping students gain feedback about how to avoid making simple but expensive mistakes in their programs that could contribute to huge inefficiencies.

Does anyone know of such a tool?

  • 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-05-30T17:18:45+00:00Added an answer on May 30, 2026 at 5:18 pm

    First, your definition of ‘completely unnecessary’ is insufficient. It is possible that some code between the two function calls affects the result of the second function call.

    Second, this has nothing to do with recursion, the same question can apply to any function call. If it has been called before with the exact same parameters, has no side-effects, and no code between the two calls changed any data the function accesses.

    Now, I’m pretty sure a perfect solution is impossible, as it would solve The Halting Problem, but that doesn’t mean there isn’t a way to detect enough of these cases and optimize away some of them.

    Some compilers know how to do that (GCC has a specific flag that warns you when it does so). Here’s a 2003 article I found about the issue: http://www.cs.cmu.edu/~jsstylos/15745/final.pdf .

    I couldn’t find a tool for this, though, but that’s probably something Eric Lipert knows, if he happens to bump into your question.

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

Sidebar

Related Questions

The very common beginner mistake is when you try to use a class property
I get a very common crash below from the code below. I thought my
I think GridView is a very common view in many situation. As a beginner
There's a very common pattern in the implementation of many functions in the haskell
I have a very common situation. I have a file, and I need to
I have a very common situation here. And for years I haven't found if
This is a very common scenario: displaying images in a ListView which have to
This seems like a very simple and a very common problem. The simplest example
In Java IoC / DI is a very common practice which is extensively used
i've noticed that plenty of games / applications (very common on mobile builds) pack

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.