Thread: Explanation of functions needed.

  1. #1
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59

    Explanation of functions needed.

    The following is stated in the book I am reading at the moment.
    Code:
    int factorial(int x)
    {
      int i;
      for(i=1; i < x; i++)
        x *= i;
      return x;
    }
    Code:
    int a=5, b;
    b = factorial(a);
    Gives variable b the value 120 at the end. Could someone explain to me how this is calculated?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,118
    It doesn't. The code is wrong.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by King Mir View Post
    It doesn't. The code is wrong.
    Am I right when I say the result is 25 or what exactly is wrong. Ow, yeah, maybe handy to say but it is pseudo code if I am right.

  4. #4
    Make Fortran great again Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    1,383
    It's trying to modify x, which is an input. Pretty sure while that input x is local to main(), the x referenced inside of factorial() is a different variable (but of the same name), local to factorial().

    The way it's supposed to work though is you get an input, say 7, and multiply 7 by every integer between 1 and 7...that code would take the x (7) and multiply it by 1, store it to x...take x, multiply by 2, store, take x, multiply by 3, store, up until 6 (7 < 7 breaks the loop), effectively giving you 7 * 6 * 5 * 4 * 3 * 2 * 1.

  5. #5
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by Epy View Post
    It's trying to modify x, which is an input. Pretty sure while that input x is local to main(), the x referenced inside of factorial() is a different variable (but of the same name), local to factorial().

    The way it's supposed to work though is you get an input, say 7, and multiply 7 by every integer between 1 and 7...that code would take the x (7) and multiply it by 1, store it to x...take x, multiply by 2, store, take x, multiply by 3, store, up until 6 (7 < 7 breaks the loop), effectively giving you 7 * 6 * 5 * 4 * 3 * 2 * 1.
    I'm an idiot. Now I see it.

    Thanks for the help the both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 09:06 PM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 08:07 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 08:29 PM
Website Security Test