Thread: The problem for return value

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    6

    The problem for return value

    Which one will return value 24 ?
    Code:
    int f(int a)
    {
      if (a < 1)
        return 1;
      else
        return a * f(a - 1);
    }
    
    (A) f(2)
        (B) f(3)
        (C) f(4)
        (D) f(6)
    I can't understand the meaning of a*f(a-1) and the whole logic. Can everyone tell me??
    Thanks!!!
    Last edited by Salem; 12-11-2014 at 12:04 AM. Reason: fixed train-wreck formatting

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The formatting of your post is atrocious. Just post your question in plain text, with any actual code in \[code\] tags.

    Read up on recursion, here's the site's lesson on it: Recursion in C and C++ - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    6
    Sorry, guys.
    I write it in correct way.
    Code:
    int f(int a)
    {
         if (a<1)
         return 1;
         else
         return a*f(a-1);
    }
    I can't understand the meaning of a*f(a-1) and the whole logic.


    Which one will return value 24?

    (A) f(2)
    (B) f(3)
    (C) f(4)
    (D) f(6)
    Last edited by tzungshianlin; 12-11-2014 at 09:20 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Formatting is much better, but still needs a little work:
    Code:
    int f(int a)
    {
        if (a<1)
            return 1;
        else
            return a*f(a-1);
    }
    Did you read the link nonpuz provided you in post #2? Click the red text.

    Next, get a piece of paper and pencil, and trace the code by hand for each choice A-D. Start with A and tell us what you think f(2) returns, and how you figured that out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with return value in C
    By jericjones45 in forum C Programming
    Replies: 8
    Last Post: 03-27-2010, 05:17 PM
  2. Problem with change return problem.
    By brian75 in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2009, 06:07 PM
  3. Problem with return
    By -Prime- in forum C Programming
    Replies: 4
    Last Post: 10-10-2006, 02:52 PM
  4. return problem
    By peterx in forum C Programming
    Replies: 3
    Last Post: 10-09-2005, 07:47 AM
  5. Return Interface: problem
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2002, 05:03 PM