One of the recent programs that was used as an example to help explain how a recursive function works used this program:
I understand how to get a factorial: 4=1*2*3*4 right but how does: a*=factorial(a-1) do that. As of yet I have figured that you are multiplying a to the function factorial(a-1) and assigning the value to a and returning it. I get lost right there, for one thing how do you use call a function within itself but with different arguments/parameters, and if doing that makes it different then how can you have that without declaring the prototype?Code:#include <stdio.h> unsigned int f,x=0; unsigned int factorial(unsigned int a); int main(void) { puts("Please enter an integer value from 1 and 8: "); scanf("%d", &x); if(x>8||x<1) { puts("Only values from 1 to 8 are acceptable."); } else { f=factorial(x); printf("%u factorial equals %u\n\n", x,f); } return 0; } unsigned int factorial(unsigned int a) { if(a==1) { return 1; } else { a *=factorial(a-1); } return a; }
And finally I have not learned about factorials I just looked some stuff up on the net and that is how I found the forumla, could someone explain what the point of factorials are or even the point of a "recursive function".
I know this is a lot to ask so if someone could just answer one or two of them I would be grateful.
P.S. The program works just fine I just don't understand it.
Also if a moderator sees this can you tell me why I can no longer log in, I tried it with my user name: cap and my pass which I tried a few times and I know is correct but it keeps saying that I do not have the correct pass, I already e-mailed for my pass to be sent to me but I also wanted this question answered, thanks.



LinkBack URL
About LinkBacks


