C语言中如何调用文件中的自定义函数

2024-11-02 10:24:28
推荐回答(1个)
回答1:

在主函数中定义即可,如下示例:
#include
void
main()
{
int
n1,n2;
long
a;
long
sum(int
a,int
b);
long
factorical(int
n);
printf("Enter
n1
and
n2:");
scanf("%d,%d",&n1,&n2);
a=sum(n1,n2);
printf("a=%ld",a);
getch();
}
long
sum(int
a,int
b)
{
long
c1,c2;
c1=factorical(a);/*n1把值传递给a,并调用函数factorical求某个数的阶乘*/
c2=factorical(b);/*n2把值传递给b,并调用函数factorical求某个数的阶乘*/
return
(c1+c2);/*返回c1与c2的和至主函数*/
}
long
factorical(int
n)
{
int
i;
long
rtn=1;
for(i=1;i<=n;i++)
rtn*=i;
return
(rtn);
}