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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:06:17+00:00 2026-05-28T15:06:17+00:00

I was reading this post on tail recursion. I’ll copy the posted solution: unsigned

  • 0

I was reading this post on tail recursion.

I’ll copy the posted solution:

unsigned int f( unsigned int a ) {
   if ( a == 0 ) {
      return a;
   }
   return f( a - 1 );   // tail recursion
}

I was wondering, what about if the result depends on several recursive function calls?
For example:

unsigned int f( unsigned int a ) {
    if ( a == 0 ) {
          return a;
       }
    return f(a -1) + f( a - 1 );   
}

Would the code above be optimized by the compiler?

  • 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-28T15:06:18+00:00Added an answer on May 28, 2026 at 3:06 pm

    As it stands, tail recursion doesn’t apply. But if you look at the end of the second answer on the question you linked to, you can see how to rewrite the function appropriately. Starting with

    unsigned int f( unsigned int a ) {
        if ( a == 0 ) {
              return a;
        }
        return f(a-1) + f(a-1);   
    }
    

    rewrite it as follows:

    unsigned int f( unsigned int a ) {
        if ( a == 0 ) {
              return a;
        }
        return 2 * f(a-1);  
    }
    

    Even now, tail recursion still isn’t directly applicable. We need to make sure the return is strictly of the form return f(....). Rewrite the function again:

    unsigned int f( unsigned int a, unsigned int multiplicative_accumulator = 1 ) {
        if ( a == 0 ) {
              return multiplicative_accumulator * a;
        }
        return f(a-1, multiplicative_accumulator * 2 );   
    }
    

    Now, tail recursion is applicable. This uses a default value for the multiplicative_accumulator (thanks @Pubby) in order that the first call to f can simply be f(x), otherwise you would have to write something f(x,1).

    A couple of final notes thanks to @SteveJessop:

    • It was safe to change f(a+1)+f(a+1) to 2*f(a+1) because f has no side effects (printing, modifying the heap, that kind of thing). If f did have side effects, that rewrite wouldn’t be valid.
    • The original was equivalent to (2*(2*(2*a)) (or more precisely, (((a+a)+(a+a))+((a+a)+(a+a)))) whereas the current version is more like (((2*2)*2)*a). This is fine, especially for ints, because multiplication is associative and distributive. But this wouldn’t be exact equivalent for float, where you would probably get small rounding discrepancies. With floating point arithmetic, sometimes a*b*c can be slightly different from c*b*a.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading this thread/post: https://stackoverflow.com/questions/262298/windows-c-ui-technology and am also wondering about a non .NET
I was reading this post While or Tail Recursion in F#, what to use
Reading this post has left me wondering; are nightly builds ever better for a
I've recently heard about the CaptureStackBackTrace function by reading this post . I cannot
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
I've been reading this post on the android developer blog about the twitter client
Dears , i was reading this post about adding the meta tags dynamically by
I was reading this post about branch per feature and this one too and
I was reading this post about the Rails 3.1 asset pipeline and the author
I was reading this post and it left me wondering... Why is it wrong

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.