Thread: what this recursive function does ?

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    what this recursive function does ?

    Code:
    void dosomething(int a){
    if (!a) return;
    dosomething(a >> 0x1);
    printf("%d\",(a & 0x1));
    //giving that int a is a positive number

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't know, why don't you compile it and try it out? See what output you get for different inputs, and see if you can figure it out. If you're still stumped, post back with your best guess and explanation, and we'll help you sort out where you went wrong. Also, the string for printf looks incorrect.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is also a missing closing brace, so the code would not even compile.

    In addition to anduril's suggestion, try explaining what the individual statements and expressions do. Do you know what bit shifting and bitwise "and" achieve? If not, look them up, otherwise you will have no hope of explaining the code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Is the printf() reachable in this code? It looks like it's set up to give a binary printing of an int, but the printf() will need to be reachable to do that.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Is the printf() reachable in this code?
    It seems like it would be a good exercise for you to do as well then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Salem View Post
    > Is the printf() reachable in this code?
    It seems like it would be a good exercise for you to do as well then
    I just did it, but I'm not entirely sure if my explanation for what I saw is correct. It does reach the printf(), but it does so only after returning (when the variable has reached 0).

    The only explanation I could think of is that the function is returning you to itself (?), and that it will do this as many times as the recursion was called.

    I need to get out of here, you people are trying to brainwash me with book-learn'in! :P

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alpo View Post
    I just did it, but I'm not entirely sure if my explanation for what I saw is correct. It does reach the printf(), but it does so only after returning (when the variable has reached 0).

    The only explanation I could think of is that the function is returning you to itself (?), and that it will do this as many times as the recursion was called.
    As Salem said, you need to do the exercise as well.

    The point of a recursive function is that it calls itself sequentially and, when it returns, backs out sequentially. All you've described is that happens on the most deeply nested call. You also need to consider that, to return control to the (original caller) each call of the recursive function needs to return.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Idan Damri View Post
    Code:
    void dosomething(int a){
    if (!a) return;
    dosomething(a >> 0x1);
    printf("%d\",(a & 0x1));
    //giving that int a is a positive number

    I decided it someone idea of code to test compiler optimization.

    I wander what compiler does the best job optimizing it.

    NOTE: I think you would need to change the input to unsigned int to get rid of undefined stuff.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by stahta01 View Post
    NOTE: I think you would need to change the input to unsigned int to get rid of undefined stuff.
    Or assume that the supplied argument is positive. Of course, the word "assume" is a contraction of "make an ASS out of yoU and ME" when it is invalid.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-18-2013, 11:01 PM
  2. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. Recursive function
    By DISGUISED in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2002, 08:23 PM