c語言fact函數(shù)怎么調(diào)用

小億
1186
2023-08-08 04:20:50
欄目: 編程語言

在C語言中,可以通過以下方式調(diào)用fact函數(shù):

  1. 在另一個(gè)函數(shù)中調(diào)用fact函數(shù):
#include <stdio.h>
// 聲明fact函數(shù)
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 調(diào)用fact函數(shù)
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// 定義fact函數(shù)
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}
  1. 在同一個(gè)函數(shù)中調(diào)用fact函數(shù):
#include <stdio.h>
// 聲明fact函數(shù)
int fact(int n);
int main() {
int num = 5;
int result = fact(num); // 調(diào)用fact函數(shù)
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// 定義fact函數(shù)
int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}

無論是在同一個(gè)函數(shù)中調(diào)用還是在不同函數(shù)中調(diào)用,都需要先聲明fact函數(shù),再進(jìn)行調(diào)用。

0