# Thread: Functions,factorials asap help needed! :(

1. ## Functions,factorials asap help needed! :(

Hi there!
I have to make c porgram, that finds factorial of number. Using defined function find and print factorials for whole numbers from 1 to n, up to the maximum possible integer value.
Hope for some help!

Here's my try, but i dont know how ti make it with my own fuctions:
Code:
``` #include<stdio.h>
int main(){
long f=1;
int i,num,min=0,max;

printf("Type n: ");
scanf("%d",&max);

printf("Factorial 1-n: ");
for(num=min;num<=max;num++){
f=1;

for(i=1;i<=num;i++)
f=f*i;

printf("%ld ",f);
}

return 0;
}```

Just rename main() ...

Code:
```int my_defined_function(){
/* YOUR EXACT CODE AS ABOVE */
}```
Code:
```int main(void) {
my_defined_function();
return 0;
}```

3. There is an incidental concern that the factorial of 13 will overflow a 32 bit integral type.

4. ## whats wrong now?

Originally Posted by grumpy
There is an incidental concern that the factorial of 13 will overflow a 32 bit integral type.
Code:
```#include<stdio.h>
int main(){funkcija();
long f=1;
int i,num,min=0,max;
int funkcija (){

printf("Type n: ");
scanf("%d",&max);

printf("Factorial 1-n: ");
for(num=min;num<=max;num++){
f=1;

for(i=1;i<=num;i++)
f=f*i;

printf("%ld ",f);
}

return 0;
}```

5. > whats wrong now?
Study qny's post again.

You've nested the definition of funkcija INSIDE main - this is wrong.

6. Originally Posted by Salem
> whats wrong now?
Study qny's post again.

You've nested the definition of funkcija INSIDE main - this is wrong.
It shows errors Can somebody put this together for me?

7. Originally Posted by Solarwin
It shows errors Can somebody put this together for me?
That would rob you of the learning experience. Did you try to do as Salem suggested? If so, show your updated code.

It seems you're having trouble with the basics of functions. Perhaps a brief read-up is in order. Here is one possible source: Functions in C - Cprogramming.com

8. Originally Posted by Matticus
That would rob you of the learning experience. Did you try to do as Salem suggested? If so, show your updated code.

It seems you're having trouble with the basics of functions. Perhaps a brief read-up is in order. Here is one possible source: Functions in C - Cprogramming.com
Yup, i tried. This is what i did:
Code:
```#include<stdio.h>
int funkcija(){
int main()
long f=1;
int i,num,min=0,max;
int funkcija (){

printf("Type n: ");
scanf("%d",&max);

printf("Factorial 1-n: ");
for(num=min;num<=max;num++){
f=1;

for(i=1;i<=num;i++)
f=f*i;

printf("%ld ",f);
}

return 0;
}```

9. You're welcome - read through it well. Take your time and make sure you understand the concepts. Note that "main()" is also a function. Let us know if you need clarification on anything.

10. Thank you all for your fast replies. But that code i entered before still have some errors:/

11. You're welcome ... but fast replies from you indicate that you're not taking our advice, not reading up on why your program has errors, and not putting in a whole lot of effort.

I'll spare you the trouble of clicking the link again and scrolling down a few lines to see the proper way to implement a function.

Code:
```#include <stdio.h>

/* function declaration */
return-type function_1(data-type variable);

int main(void)
{
// ...

function_1(x);  /* function call (actually using the function) */

return 0;
}

/* function definition */
return-type function_1(data-type variable)
{
// ...
}```

12. For what it's worth, given the original problem I think the idea is to write a function that will compute a factorial, not to write a function that implements the whole program. Something like this, perhaps...
Code:
`long factorial(int n);`
Still, what you're doing now is good practice and you may want to finish that first.

13. Originally Posted by dwks
For what it's worth, given the original problem I think the idea is to write a function that will compute a factorial, not to write a function that implements the whole program. Something like this, perhaps...
Code:
`long factorial(int n);`
Still, what you're doing now is good practice and you may want to finish that first.
This is my homework that i have to send in few hours!:/Could u send me email with your code? i will do practise anyway

14. Sorry but no, we don't directly do homework on this forum. Anyway I was just guessing based on the problem type, you weren't actually clear enough in the original post to figure out which interpretation is required by the assignment.

Here are some thoughts. A function is a separate piece of code that you can run/invoke/call by its name. For this to work, you need a function prototype (name of function with arguments and a semicolon), which declares the function and lets you call it after that point. You need an actual function definition, which has the code within the function that will run when you call it. And then you need the function call itself, which is the point (e.g. in main) that you want to jump to the function's code and run. When the function is done, it will return to the original calling location. Example:
Code:
```void two(void);

int main() {
printf("1\n");
two();
printf("3\n");
return 0;
}

void two(void) {
printf("2\n");
}```
That prints 1, 2, 3. There are two other major things you can do with functions. The first is that you can pass in arguments,
Code:
```void print(int x);

int main() {
printf("1\n");
print(2);
printf("3\n");
return 0;
}

void print(int x) {
printf("%d\n", x);
}```
and the second is that you can return values.
Code:
```int important(void);

int main() {
int i = important();
printf("the function thinks this is important: %d\n", i);
return 0;
}

int important(void) {
return 42;
}```
Of course, the two can be combined together for something like
Code:
`long factorial(int n);`