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

  • Home
  • SEARCH
  • 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 8121641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:33:32+00:00 2026-06-06T05:33:32+00:00

I have a little confusion in the following two statements. The below program is

  • 0

I have a little confusion in the following two statements.

The below program is finds the index of an element out of a sorted array[no duplicates] using binary search.

int bin(int *arr,int l,int h,int k)
{
    int mid;
    if(l>h)
            return -1;
    if(l==h)
    {
            return arr[l]==k?l:-1;
    }
    else
    {
            mid=(l+h)>>1;
            if(arr[mid]==k)
                    return mid;
            else if(k>arr[mid])
                    bin(arr,mid+1,h,k);
            else
                    bin(arr,l,mid-1,k);
    }
}

I do not have any problem in the program[working perfectly]:

I have confusion in following two statements:

bin(arr,l,mid-1,k); http://ideone.com/p1o5U
return bin(arr,l,mid-1,k); http://ideone.com/lMhgB

Using any of the above statement gives correct result.
Which statement is more efficient in terms of time?
How the program is working fine even without return statement?

  • 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-06T05:33:33+00:00Added an answer on June 6, 2026 at 5:33 am

    How the program is working fine even without return statement?

    In this case I’d say there is no difference in practice, as the recursion ends to a return statement irrelevant of which parameters you give to it (yes, even though you don’t specify it in the last two calls). This is however a compiler-specific detail that you should not rely on!

    Which statement is more efficient in terms of time?

    You might consider the ones with the return statement a bit more efficient as in that case it doesn’t need to read up on the “else” check, however, the difference is very small.


    Do note however that without return statements it is considered “incorrect”, even though it “works”, and -Wall should give you warnings about it.

    See this similar question also. Copied from that answer:

    What you see is probably caused by the implementation detail that
    function on some architectures return (integral values) by setting a
    well known register to that value (eax on i386). Therefore, if the
    bottommost recursive call does return and set this register, and the
    calls in-between don’t stomp on that register, you see that it sort of
    works. However, you mustn’t rely on that.

    Edit: changed the wording as to not imply the behaviour as a deliberate optimization. Don’t know if it is or not. It is anyway something that some compilers do and some don’t.

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

Sidebar

Related Questions

I have a little confusion with the following things: Null DBNull.Value When I use
The title is a little confusing so I will explain. I have the following
I have a little confusion: Is the function insertNodeInto used to inserts only a
I am working in Linux and i have a little bit confusion that whether
I have the following little import application, basically it... connects to a hosted SQL
mmm, I have just a little confusion about multiple auto declarations in the upcoming
This might get a little confusing as I have tried everything to make this
the title might be a little bit confusing, let me explain, ;) I have
I have little unusual problem to solve. I need some hint or links to
I have little bit longer question for you - but hope answer will be

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.