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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:21:22+00:00 2026-05-16T17:21:22+00:00

Sometimes it’s simple enough (if the self call is the last statement, it’s tail

  • 0

Sometimes it’s simple enough (if the self call is the last statement, it’s tail recursion), but there are still cases that confuse me. A professor told me that “if there’s no instruction to execute after the self-call, it’s tail recursion”. How about these examples (disregard the fact that they don’t make much sense) :

a) This one should be tail recursive, seeing how the self-call is the last statement, and there’s nothing left to execute after it.

function foo(n)
{
    if(n == 0)
        return 0;
    else
        return foo(n-2);
}

b) But how about this one? It should be a tail call, because if the condition is true, nothing except it will be executed, but it’s not the last statement?

function foo(n)
{
    if(n != 0)
        return foo(n-2);
    else
        return 0;
}

c) How about this one? In both cases, the self call will be the last thing executed :

function foo(n)
{
    if(n == 0)
        return 0;
    else    
    {
        if(n > 100)
            return foo(n - 2);
        else
            return foo(n - 1);
    }
}
  • 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-16T17:21:23+00:00Added an answer on May 16, 2026 at 5:21 pm

    It might help you to think about this in terms of how tail-call optimisations are actually implemented. That’s not part of the definition, of course, but it does motivate the definition.

    Typically when a function is called, the calling code will store any register values that it will need later, on the stack. It will also store a return address, indicating the next instruction after the call. It will do whatever it needs to do to ensure that the stack pointer is set up correctly for the callee. Then it will jump to the target address[*] (in this case, the same function). On return, it knows the return value is in the place specified by the calling convention (register or stack slot).

    For a tail call, the caller doesn’t do this. It ignores any register values, because it knows it won’t need them later. It sets up the stack pointer so that the callee will use the same stack the caller did, and it doesn’t set itself up as the return address, it just jumps to the target address. Thus, the callee will overwrite the same stack region, it will put its return value in the same location that the caller would have put its return value, and when it returns, it will not return to its caller, but will return to its caller’s caller.

    Therefore, informally, a function is tail-recursive when it is possible for a tail call optimisation to occur, and when the target of the tail call is the function itself. The effect is more or less the same as if the function contained a loop, and instead of calling itself, the tail call jumps to the start of the loop. This means there must be no variables needed after the call (and indeed no “work to do”, which in a language like C++ means nothing to be destructed), and the return value of the tail call must be returned by the caller.

    This is all for simple/trivial tail-recursion. There are transformations that can be used to make something tail-recursive which isn’t already, for example introducing extra parameters, that store some information used by the “bottom-most” level of recursion, to do work that would otherwise be done on the “way out”. So for instance:

    int triangle(int n) {
        if (n == 0) return 0;
        return n + triangle(n-1);
    }
    

    can be made tail-recursive, either by the programmer or automatically by a smart enough compiler, like this:

    int triangle(int n, int accumulator = 0) {
        if (n == 0) return accumulator;
        return triangle(n-1, accumulator + n);
    }
    

    Therefore, the former function might be described as “tail recursive” by someone who’s talking about a smart enough language/compiler. Be prepared for that variant usage.

    [*] Storing a return address, moving the stack pointer, and jumping, may or may not be wrapped up in a single opcode by the architecture, but even if not that’s typically what happens.

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

Sidebar

Related Questions

sometimes there are sites that have some small information window thats always visible when
Sometimes, tomcat restart will cause my application stop immediately so that my consuming data
Sometimes I have to check for some condition that doesn't change inside a loop,
Sometimes I have come across this issue that whenever I change the order of
Sometimes I get errors that I suspect are the result of my Django app
I am doing a simple coin flipping experiment for class that involves flipping a
Sometimes you have a function that will work for flat arguments. For example: send(player,message)
sometimes I found that a few PMD rules conflict with each other, therefore you
Sometimes, a value must be checked for equality with a constant. In such cases,
Sometimes I clone git repo not for a development - but just for the

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.