c语言fact函数怎么调用
在C语言中,可以通过以下方式调用fact函数:
- 在另一个函数中调用fact函数:
#include <stdio.h>
// 声明fact函数
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 调用fact函数
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// 定义fact函数
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}
- 在同一个函数中调用fact函数:
#include <stdio.h>
// 声明fact函数
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 调用fact函数
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// 定义fact函数
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}
无论是在同一个函数中调用还是在不同函数中调用,都需要先声明fact函数,再进行调用。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:java如何计算矩形的周长和面积 下一篇:如何使用ANT与YUI压缩js
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。