新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c语言fact函数如何调用,c语言中fact()

发布时间:2023-08-09 07:57:29

c语言fact函数如何调用

在C语言中,可以通过以下方式调用fact函数:
1. 在另外一个函数中调用fact函数:
```c
#include
// 声明fact函数
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 调用fact函数
printf("Factorial of %d is %d ", num, result);
return 0;
}
// 定义fact函数
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}
```
2. 在同一个函数中调用fact函数:
```c
#include
// 声明fact函数
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 调用fact函数
printf("Factorial of %d is %d ", num, result);
return 0;
}
// 定义fact函数
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}
```
不管是在同一个函数中调用或在区分函数中调用,都需要先声明fact函数,再进行调用。