Someone explain this to me

This is a discussion on Someone explain this to me within the C Programming forums, part of the General Programming Boards category; how does this work: Code: #include <stdio.h> int fact(int n){ static int num = 0; if(n == 1) return 1; ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Someone explain this to me

    how does this work:
    Code:
    #include <stdio.h>
    
    int fact(int n){
      static int num = 0;
      if(n == 1)
        return 1;
      else{
        num++;
        printf("&#37;d \n", num); 
        return fact(n-1)*n;
      }
    }
    
    int main(void){
      printf("fact(%d) is %d\n", 5, fact(5));
      return 0;
    }
    i dont understand it to good, the return fact(n-1)*n; part

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It's an example of recursion. A function calls itself, or it could have several levels: function A calls function B which calls function A.

    If you call fact() with 0 or a negative value you're going to have a runaway condition. Otherwise the code looks OK on quick inspection. I haven't compiled it to check.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i dont understand how it works though, does it do n-1 = 4 * 5 = 20 the first time? if so what does it do the second time?

    i put the static int num to see how much times the function is used but i still dont entirely understand how it works

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Output:
    Code:
    1 
    2 
    3 
    4 
    fact(5) is 120
    Yes. It is in fact doing 5 * 4 * 3 * 2 * 1
    which is 120.

    That's the definition of factorial.
    Mathematically written it's 5! = 120

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    oh, makes sense now

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Excellent! You get the idea.

    I was wrong with the above - because it's not 100&#37; accurate reading it left-to-right.

    In fact it's doing it in this order
    (((1 * 2) * 3) * 4) * 5

    Because factorial is defined as n x (n - 1) in short-form mathematical terms, it's used often to introduce recursive function calls in programming. Ironically it's probably the worst example of the need for recursion because a simple loop with a count-down is much more efficient.

    Still, it expands the mind as to possibilities wherein recursion may serve elegantly with other problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 02:16 PM
  2. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 01:22 AM

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