c语言返回计算问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 12:09:07
int total(int a,int b)
{ int start,stop,i,sum=0;
start=a;
stop=b;
if(a>b)
{start=b;
stop=a;
}
for(i=start;i<=stop;i++)
sum+=i;
return sum;
}
我想知道 sum返回给了谁
接下来该怎么写》。。。。。

你这是个子函数,total()可以说是在阐述一个方法,要知道sum的值传给哪里,需要用main()调用下,下面是把sum的值传递给了temp:
main()
{
int a = 2;
int b = 8;
int temp;
temp = total(2,8)
printf("\n total is %d",temp);
getch()
}
int total(int a,int b)
{ int start,stop,i,sum=0;
start=a;
stop=b;
if(a>b)
{start=b;
stop=a;
}
for(i=start;i<=stop;i++){
sum+=i;
return sum;
}
}

还有,就是你上面写的程序中for循环少了一个大括号!

main(){
int a,b,c;
a=8;
b=12;

c=total(9,6);
printf("\nTotal is %d",c);
getch();
}

total(4,5); 函数调用表达式就是函数的返回值

int a = total(3,4);将返回值赋给a

你没有给出调用语句谁知道啊,不过一般来说肯定会有个调用语句比如:
s=total(...),那么这个sum返回给了s。