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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:37:34+00:00 2026-05-24T21:37:34+00:00

I realise the answer to this question could be different for different languages, and

  • 0

I realise the answer to this question could be different for different languages, and the language I am most interested in is C++. If the tag needs be changed because this can’t be answered in a language-agnostic manner, feel free.


Is it possible to have a function be partially tail-recursive and still get any advantage that being tail-recursive would get you?

As I understand it, tail-recursion is where instead of doing a full function call, the compiler will optimise the function to just change the arguments in place to the new arguments and jump to the beginning of the function.

If you have a function like this:

def example(arg):
    if arg == 0:
        return 0 # base case

    if arg % 2 == 0:
        return example(arg - 1) # should be tail recursive

    return 3 + example(arg - 1) # isn't tail recursive because 3 is added to the result

When an optimiser encounters something like that (where the function is tail-recursive in some cases and not in others) will it turn the one into a jump and the other into a call, or will some fact of optimisation reality (if I knew it I wouldn’t be asking) make it have to turn everything into a call and lose all the efficiency you would have had if the function were tail-recursive?

  • 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-24T21:37:35+00:00Added an answer on May 24, 2026 at 9:37 pm

    In Scheme, the first language that comes to mind when I think of tail calls, the second case is guaranteed to be a tail call by the language specification. (Terminology note: it is preferred to refer to such function calls as ‘tail calls’.)

    The Scheme specification defines exactly what tail calls are in Scheme and mandates that compilers support them specially. You can see the definition in 11.20. Tail calls and tail contexts of R6RS (source).

    Note that in Scheme, the specification says nothing about optimization of tail calls. Rather, it says that an implementation must support an unbounded number of active tail calls — a semantic property of the language runtime. They can be implemented as normal calls, but usually aren’t.

    Example, in C:

    Take a C version of your example.

    int example(int arg)
    {
        if (arg == 0)
            return 0;
        if ((arg % 2) == 0)
            return example(arg - 1);
        return 3 + example(arg - 1);
    }
    

    Compile it using gcc’s usual optimization settings (-O2) for i386:

    _example:
        pushl   %ebp
        xorl    %eax, %eax
        movl    %esp, %ebp
        movl    8(%ebp), %edx
        testl   %edx, %edx
        jne L5
        jmp L15
        .align 4,0x90
    L14:
        decl    %edx
        testl   %edx, %edx
        je  L7
    L5:
        testb   $1, %dl
        je  L14
        decl    %edx
        addl    $3, %eax
        testl   %edx, %edx
        jne L5
    L7:
        leave
        ret
    L15:
        leave
        xorl    %eax, %eax
        ret
    

    Note that there are no function calls in the assembly code. GCC has not only optimized your tail call into a jump, it optimized the non-tail call into a jump as well.

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

Sidebar

Related Questions

I realise that this question may be almost impossible to answer definitively, but: Is
This question may be easy and the answer obvious, but I can't seem to
I realize that the answer to this question is likely quite obvious (if somewhat
I realize there's no definitely right answer to this question, but when people talk
I realize that this question is impossible to answer absolutely, but I'm only after
I realise that this is a very basic question, but it is one which
I realize this is a pretty open question and could get a variety of
a simple question but Ive realised im not sure of the answer for this
I realise this question has been asked 100 times, but i've looked through the
I realise this is not the ideal place to ask about this in terms

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.