函数的参数太少而且不能用作函数 – 从C开始

嗨,我是初学者,我有这个功课为我开始C课。 我一直在使用我的函数编写的程序出错。 这是我的计划:

#include  //Function Declarations double obtainTemp (void); **double convertTemp (double tempF, double tempR, double tempC, double tempK);** void printResult (double tempF, double tempR, double tempC, double tempK); int main (void) { //Local Declarations double tempF; double tempR; double tempC; double tempK; double fahrenheit; double rankine; double celsius; double kelvin; //Calling the functions fahrenheit = obtainTemp (); rankine = convertTemp (tempR); celsius = convertTemp (tempC); kelvin = convertTemp (tempK); //will print it by... printResult (tempF, tempR, tempC, tempK); int temp; printf("Press anything to exit: "); scanf("%d", &temp); return 0; }//main //============obtainTemp=============== double obtainTemp (void) { //Local Declarations double tempF; printf("Enter temperature: "); scanf("%lf", &tempF); return tempF; } //============convertTemp============== int convertTemp (double tempF, double tempR, double tempC, double tempK); { //Statements tempR = (tempF - 32) + 491.67; tempC = (tempF - 32) * 100/180; tempK = tempC + 273.16; return tempF, tempR, tempC, tempK; } //============printResult=============== void printResult (double tempF, double tempR, double tempC, double tempK) { //Statements printf("The temperature is %lf degrees fahrenheit\n", tempF); printf("The value of %lf in rankine is %lf\n", tempF, tempR); printf("The value of %lf in celsius is %lf\n", tempF, tempC); printf("The value of %lf in kelvin is %lf\n", tempF, tempK); return; } 

下面这个函数的参数太少了,编译器说我不能把它当作函数使用。 为什么哦为什么?

 double convertTemp (double tempF, double tempR, double tempC, double tempK); 

对不起,我是初学者…我真的很感谢你的帮助:)

错误很明显,你不是按照应该的方式调用函数。 该函数需要4个参数,而您只传递一个参数。

但这只是你的第一个错误。 SECOND,是因为它们现在声明的函数参数,将生成参数的本地副本:

 double convertTemp (double tempF, double tempR, double tempC, double tempK); 

这意味着在函数体内,对这些变量中的任何变量的更改都不会传播到您在以前调用convertTemp() main中声明的变量。 我所说的是在调用函数时,在堆栈上创建另外4个变量,并从发送给函数的变量中复制它们的值。

有两种方法可以解决这个问题:

  • 第一个 ,如果你对指针一无所知,要理解它有点复杂。 在这种方法中,为了修改main的原始变量,您需要更改函数签名以接收内存指针:

    void convertTemp(double * tempF,double * tempR,double * tempC,double * tempK);

并且函数体也需要改变,以便与文件开头声明的原型一致:

 void convertTemp (double* tempF, double* tempR, double* tempC, double* tempK) { //Statements *tempR = (*tempF - 32) + 491.67; *tempC = (*tempF - 32) * 100/180; *tempK = *tempC + 273.16; } 

请注意,新函数签名不返回任何值(即void )。 这不是必需的,因为您将直接操作main()传递的变量。

main() ,你应该调用这样的函数:

 fahrenheit = obtainTemp(); convertTemp(&fahrenheit, &rankine, &celsius, &kelvin); 
  • 第二种方法 ,因为你是初学者,这可能会让你更容易理解,就是声明3个函数,每个函数需要做一次:

 double convertR(double value) { return (value - 32) + 491.67; } double convertC(double value) { return (value - 32) * 100/180; } double convertK(double value) { return value + 273.16; } 

然后在main() ,你会称它们为:

 fahrenheit = obtainTemp(); rankine = convertR(fahrenheit); celsius = convertC(fahrenheit); kelvin = convertK(fahrenheit); printResult(fahrenheit, rankine, celsius, kelvin); 

在C中,您必须匹配函数声明的参数数量。 如果要在函数中支持可变数量的参数,可以使用stdarg 。 所以你的编译器告诉你:

 rankine = convertTemp(tempR); 

没有4个参数,但你的声明确实如此。

您必须传递函数所需的参数数量。 convertTemp需要4个参数, tempFtempRtempCtempK 。 你只是在调用convertTemp传入一个参数。

您可能需要编写三个版本的convertTempconvertFahrenheitToRankineconvertFahrenheitToCelsius ,和convertFahrenheitToKelvin 。 这些函数中的每一个都应该采用一个双参数,即华氏温度作为输入,每个参数应输出从华氏温度转换为其转换的单位类型。

以下函数参数太少,

你告诉编译器它需要四个参数

 double convertTemp (double tempF, double tempR, double tempC, double tempK); 

但你只是在这里传递一个。

 rankine = convertTemp (tempR); celsius = convertTemp (tempC); kelvin = convertTemp (tempK); 

我建议你注释掉你的大部分代码,并初始化你的双打,就像这样。

 #include  //Function Declarations //double obtainTemp (void); //**double convertTemp (double tempF, double tempR, double tempC, double tempK);** void printResult (double tempF, double tempR, double tempC, double tempK); int main (void) { //Local Declarations double tempF = 0.0; double tempR = 0.0; double tempC = 0.0; double tempK = 0.0; // double fahrenheit; // double rankine; // double celsius; // double kelvin; //Calling the functions // fahrenheit = obtainTemp (); // rankine = convertTemp (tempR); // celsius = convertTemp (tempC); // kelvin = convertTemp (tempK); // //will print it by... printResult (tempF, tempR, tempC, tempK); int temp; // printf("Press anything to exit: "); // scanf("%d", &temp); return 0; }//main //============obtainTemp=============== //double obtainTemp (void) //{ // //Local Declarations // double tempF; // printf("Enter temperature: "); // scanf("%lf", &tempF); // // return tempF; //} // //============convertTemp============== //int convertTemp (double tempF, double tempR, double tempC, double tempK); //{ // // //Statements // tempR = (tempF - 32) + 491.67; // tempC = (tempF - 32) * 100/180; // tempK = tempC + 273.16; // // return tempF, tempR, tempC, tempK; //} // //============printResult=============== void printResult (double tempF, double tempR, double tempC, double tempK) { //Statements printf("The temperature is %f degrees fahrenheit\n", tempF); printf("The value of %f in rankine is %f\n", tempF, tempR); printf("The value of %f in celsius is %f\n", tempF, tempC); printf("The value of %f in kelvin is %f\n", tempF, tempK); return; } 

这应该编译有关未使用变量的最小警告。 接下来,取消注释并更正最简单的事情,然后是下一个最简单的事情,依此类推。 尝试编译而不发出警告。

您需要使用主函数中的4个参数调用convertTemp ,而不是一个。 我想..但我不确定你想要同时返回所有3个值。 如果是这样,您必须重新定义函数以使用指针而不是固定值。

 int convertTemp (double tempF, double *tempR, double *tempC, double *tempK); { //Statements *tempR = (tempF - 32) + 491.67; *tempC = (tempF - 32) * 100/180; *tempK = *tempC + 273.16; return 0; // return 0 for ok? in your function declaration you said it to be and double instead of a int } 

然后你需要从你这里打电话给他们:

 //Calling the functions fahrenheit = obtainTemp (); if (convertTemp (fahrenheit, &tempR, &tempC,&tempK) == 0) { printResult (fahrenheit, tempR, tempC, tempK); } 

使用变量前面的&运算符,我们告诉编译器给出函数的内存地址而不是变量本身的值。 现在该函数具有地址,它可以dereference指针(内存地址),因此它可以更改主函数中变量的内容:)