大家好,我是一名初学者,我有一份 C 类(class)的家庭作业。对于我编写的程序,尤其是使用我的函数,我不断收到错误。这是我的程序:
#include <stdio.h>
//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 个参数,而您只传递一个。
但这只是你的第一个错误。第二个是函数参数,正如它们现在声明的那样,将制作参数的本地副本:
double convertTemp (double tempF, double tempR, double tempC, double tempK);
这意味着在函数体内,对任何这些变量的更改都不会传播到您用来调用 convertTemp()
的 main 中声明的变量。我的意思是在调用函数时,在堆栈上创建了另外 4 个变量,它们的值是从您发送给函数的变量中复制的。
有两种方法可以解决这个问题:
第一个,如果您对指针一无所知的话,理解起来会稍微复杂一些。在这种方法中,为了修改 main 的原始变量,您需要更改函数签名以接收内存指针:
void convertTemp (double* tempF, double* tempR, double* tempC, double* tempK);
并且函数体也需要改变,以与文件开头声明的原型(prototype)保持一致:
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 - 函数的参数太少,不能用作函数----以 C 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6038713/