-
Function Help
Code:
#include <stdio.h>
int main(void)
{
int n, i, sum;
printf("Enter a value for n!: ");
scanf("%d", &n);
sum = n;
for( i = 1; i < n; i++ )
sum = sum * ( n - i );
printf("n! is %d\n", sum);
}
In main() you will still prompt the user for a positive integer, n, but then you will pass it to a function that calculates n! and then returns the value to main() for printing.
How do I change this into a function?
-
Have you looked at functions at all? In your book? In tutorials?
-
A little bit, why?
But what is a good tutorials site?
-
Why? Because you said you wanted to make some of your code into a function. So we check that you even know what a function is. You can look at this very site for some tutorials. Essentially whatever code calculates your value (in this case, n!) you put in a separate function to be called from whatever piece of code needs it.