C 函数声明中的参数名称(不含类型)
所以,我正在做作业,我的老师没有很好地解释函数。我将节省一些时间,并显示发生错误的主要部分:C 函数声明中的参数名称(不含类型),c,C,所以,我正在做作业,我的老师没有很好地解释函数。我将节省一些时间,并显示发生错误的主要部分: #include <stdio.h> 6 int main(void) 7 { 8 double Depth; 9 double celsius_at_depth(Depth); 10 { 11 10*(Depth)+20; 12 } 很抱歉格式化,我希望它更容易查看。double不应该是函数的参数类型吗 编辑:我在网站上查找了错误,但是我没有看到与代码格式相
#include <stdio.h>
6 int main(void)
7 {
8 double Depth;
9 double celsius_at_depth(Depth);
10 {
11 10*(Depth)+20;
12 }
很抱歉格式化,我希望它更容易查看。double不应该是函数的参数类型吗
编辑:我在网站上查找了错误,但是我没有看到与代码格式相同的错误,所以我觉得最好发布一个新的在另一个函数中定义一个函数是gccAFAIK的非标准扩展,所以这不是一个好主意 要声明该函数,首先需要将其移出
main()
so
double celsius_at_depth(double depth);
然后你可以这样称呼它
#include <stdio.h>
int main(void)
{
double depth = 10;
printf("celsius_at_depth(%f) = %f\n", depth, celsius_at_depth(depth))
return 0;
}
这里您使用double作为函数的返回类型,但在函数中未使用return。这是一个错误
#include <stdio.h>
int main(void)
{
double Depth;
double celsius_at_depth(Depth);
{
return (10*(Depth)+20);
}
//also call the funciton for returning the value
celsuis_at_depth(Depth) ;
printf("is the degree celcius \n");
}
#包括
内部主(空)
{
双深度;
深度处的双摄氏度(深度);
{
返回(10*(深度)+20);
}
//也可以调用函数来返回值
celsuis_在_深度(深度);
printf(“是celcius的学位”);
}
这条线的深度是多少代码>用于?您是否打算使用“celcius_at_depth”功能?你需要在main
@BinaryJudy:不,他需要在main
之外定义它。最好也在main
之外声明它,但是允许使用局部函数声明。(函数定义包括{…}
;声明不包括在内。)@KeithThompson declarion不是问题,但代码表明OP是在main()
中定义的。“我的老师没有很好地解释函数。”然后你需要在教科书中亲自阅读。如果你不能百分之百地理解函数,那么用这个练习解决眼前的问题绝对是毫无意义的。函数是命令式语言中最重要的抽象之一,因此您需要非常熟悉它们。如果您对老师的解释不满意,请阅读一本书,并独立做一些练习。您如何处理深度处的Celsiu返回值?
?这是OP的问题,但肯定很烦人,我会解决它。在main之前声明或定义函数。
double celsius_at_depth(double depth);
{
return 10 * depth + 20;
}
#include <stdio.h>
int main(void)
{
double Depth;
double celsius_at_depth(Depth);
{
return (10*(Depth)+20);
}
//also call the funciton for returning the value
celsuis_at_depth(Depth) ;
printf("is the degree celcius \n");
}