Explanation of functions needed.

This is a discussion on Explanation of functions needed. within the C Programming forums, part of the General Programming Boards category; The following is stated in the book I am reading at the moment. Code: int factorial(int x) { int i; ...

  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,023
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    960
    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, 03:25 AM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21