Results 1 to 4 of 4

Thread: Return value for a leaf function

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Answers
    1

    Return value for a leaf function

    In the following example, what would be printed to the screen: four? Will there be any valuable output at all? In other words, my question is wheter or not will the value of the statement "leaf(result);" be the returned value?

    main(){
    printf("%d", leaf(5));
    }

    int leaf(int f){
    int result;
    result = f-1;
    if (f<5)
    return result;
    leaf(result);
    }

    Thank you,
    Tomer.


  2. #2
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: Return value for a leaf function

    Reformatting and adding some line numbers to assist in the discussion below
    Code:
    /*  1 */ int leaf(int f)
    /*  2 */ {
    /*  3 */     int result;
    /*  4 */     result = f-1;
    /*  5 */     if (f<5)
    /*  6 */         return result;
    /*  7 */     leaf(result);
    /*  8 */}
    As written, this should invoke undefined behavior per section 6.9.1, paragraph 12:

    If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
    The first time leaf is called from printf, the value of the parameter f is 5. (f < 5) evaluates to false, so execution skips to line 7 and leaf is called again. This time, the value of the parameter f is 4. (f < 5) evaluates to true, so line 6 is executed, returning the value 3. Execution picks up at line 8, which is the closing }.

    A graphical representation of the call tree should help:
    Code:
    printf("%d", leaf(5));
    
        leaf(5)                // call from printf
        {                       
            int result;         
            result = 4;       
            if (5 < 5)         
            leaf(result);     
    
              leaf(4)          // call from leaf(5)
              {                 
                int result;    
                result = 3;   
                if (4 < 5)     
                  return 3;   // return to leaf(5)
    
       }                         // return to printf
    Since execution has reached the closing } and the value of the function call is being used by the caller, the resulting behavior is undefined.

    Since the behavior is undefined, anything can happen at this point. You may get an output of 3. You may get an output of 4. You may get garbage. You may get no output at all.

    The fix is easy. Instead of writing
    Code:
    if (f < 5)
      return result;
    leaf(result);
    use
    Code:
    if (f < 5)
      return result;
    return leaf(result);
    or
    Code:
    if (f < 5)
      return result;
    else
      return leaf(result);



  3. #3
    Junior Member
    Join Date
    Mar 2011
    Answers
    6

    Re: Return value for a leaf function

    I think it would print 3 because once condition is met, control will return with the value 3


  4. #4
    Junior Member
    Join Date
    Apr 2011
    Answers
    13

    Re: Return value for a leaf function

    yes it will print 3


  5.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact