温馨提示×

c语言fact函数怎么调用

小亿
945
2023-08-08 04:20:50
栏目: 编程语言

在C语言中,可以通过以下方式调用fact函数:

  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);
}
}
  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函数,再进行调用。

0