C语言 函数的参数太少,不能用作函数----以 C 开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6038713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
too few arguments to function and can't be used as a function---- beginning C
提问by redkimono
Hi i am a beginner, and I have this homework for my beginning C class. I keep getting errors for the program I wrote particularly with my function. Here's my program:
嗨,我是初学者,我的初学 C 课有这个作业。我一直在为我编写的程序出错,特别是我的函数。这是我的程序:
#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;
}
This function below has too few arguments, and compiler says i can't use it as a function. why oh why?
下面这个函数的参数太少,编译器说我不能将它用作函数。为什么哦为什么?
double convertTemp (double tempF, double tempR, double tempC, double tempK);
Sorry, I am a beginner...i would really appreciate your help :)
抱歉,我是初学者……非常感谢您的帮助:)
采纳答案by karlphillip
The error is pretty clear, you're not calling the function the way it's supposed to be. The function takes 4 parameters, and you are only passing one.
错误很明显,您没有按照应有的方式调用该函数。该函数需要 4 个参数,而您只传递了一个参数。
But that is only your FIRST mistake. The SECOND, is that the function arguments as they are declared right now, will make a local copy of the parameters:
但这只是你的第一个错误。第二,现在声明的函数参数将制作参数的本地副本:
double convertTemp (double tempF, double tempR, double tempC, double tempK);
This means that inside the body of the function, changes on any of these variables will not propagate to the variables declared in main which you used to call convertTemp(). What I'm saying is at the time the function is called, another 4 variables are created on the stack and their values are copied from the variables you sent to the function.
这意味着在函数体内部,对这些变量中的任何一个的更改都不会传播到 main 中声明的变量,您曾经调用convertTemp(). 我要说的是在调用函数时,在堆栈上创建了另外 4 个变量,它们的值是从您发送给函数的变量中复制的。
There are two approaches to solve this problem:
有两种方法可以解决这个问题:
The first, a little more complex to understand if you don't know nothing about pointers. On this approach, in order to modify the original variables of main, you need to change your function signature to receive memory pointers instead:
void convertTemp (double* tempF, double* tempR, double* tempC, double* tempK);
第一个,如果您对指针一无所知,理解起来会更复杂一些。在这种方法中,为了修改 main 的原始变量,您需要更改函数签名以接收内存指针:
void convertTemp (double* tempF, double* tempR, double* tempC, double* tempK);
and the body of function needs to change too, in order to be consistent with the prototype declared in the beginning of the file:
并且函数体也需要改变,以与文件开头声明的原型一致:
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;
}
Note that the new function signature does not return any value (ie. void). This is not necessary since you will be operating directly on the variables passed by main().
请注意,新函数签名不返回任何值(即void)。这不是必需的,因为您将直接对传递的变量进行操作main()。
On main(), you should call the function like:
On main(),您应该调用函数,如:
fahrenheit = obtainTemp();
convertTemp(&fahrenheit, &rankine, &celsius, &kelvin);
- The second approach, since you are a beginner this is probably going to be easier for you to understand, is to declare 3 functions, one for each conversion you need to do:
- 第二种方法,由于您是初学者,这对您来说可能更容易理解,是声明 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;
}
Then on main(), you would call them like:
然后main(),你会这样称呼它们:
fahrenheit = obtainTemp();
rankine = convertR(fahrenheit);
celsius = convertC(fahrenheit);
kelvin = convertK(fahrenheit);
printResult(fahrenheit, rankine, celsius, kelvin);
回答by Mel
In C you have to match the number of arguments to your function declaration. If you want to support a variable number of arguments in your function, you use stdarg. So your compiler is telling you that:
在 C 中,您必须将参数的数量与函数声明相匹配。如果要在函数中支持可变数量的参数,请使用stdarg。所以你的编译器告诉你:
rankine = convertTemp(tempR);
Does not have 4 arguments, but your declaration does.
没有 4 个参数,但您的声明有。
回答by Ben Burns
You must give pass in the number of arguments that a function requires. convertTemprequires 4 arguments, tempF, tempR, tempC, tempK. You're only passing in one argument in your call to convertTemp.
您必须传入函数所需的参数数量。 convertTemp需要 4 个参数,tempF, tempR, tempC, tempK。您只在调用convertTemp.
Chances are you need to write three versions of convertTemp. convertFahrenheitToRankine, convertFahrenheitToCelsius, and convertFahrenheitToKelvin. Each one of these functions should take one double argument which is a temperature in fahrenheit as an input, and each one should output the conversion from fahrenheit to the unit type for which it converts.
您可能需要编写三个版本的convertTemp. convertFahrenheitToRankine、convertFahrenheitToCelsius、 和convertFahrenheitToKelvin。这些函数中的每一个都应该采用一个双参数,即华氏温度作为输入,并且每个函数都应该输出从华氏温度到它所转换的单位类型的转换。
回答by Mike Sherrill 'Cat Recall'
This function below has too few arguments,
下面这个函数的参数太少,
You're telling the compiler it takes four arguments here
你告诉编译器这里需要四个参数
double convertTemp (double tempF, double tempR, double tempC, double tempK);
But you're only passing one here.
但你在这里只通过了一个。
rankine = convertTemp (tempR);
celsius = convertTemp (tempC);
kelvin = convertTemp (tempK);
I suggest you comment out most of your code, and initialize your doubles, like this.
我建议您注释掉大部分代码,并像这样初始化双打。
#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 = 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;
}
That should compile with minimal warnings about unused variables. Next, uncomment and correct the simplest thing, then the next simplest thing, and so on. Try to compile without warnings.
这应该在编译时对未使用的变量发出最少的警告。接下来,取消注释并更正最简单的事情,然后是下一个最简单的事情,依此类推。尝试在没有警告的情况下编译。
回答by DipSwitch
You need to call convertTempwith 4 parameters from your main function and not one. I think.. but i'm not sure you want to return all 3 values at the same time. If so you have to redefine your function to use pointers instead of fixed values.
您需要convertTemp从主函数中调用4 个参数而不是一个。我想.. 但我不确定你想同时返回所有 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
}
then you need to call them from you main like:
那么你需要从你的主要调用它们,比如:
//Calling the functions
fahrenheit = obtainTemp ();
if (convertTemp (fahrenheit, &tempR, &tempC,&tempK) == 0) {
printResult (fahrenheit, tempR, tempC, tempK);
}
With the &operator in frond of the variable we tell the compiler to give the memory address of the function and not the value of the variable itself. Now the function has the addresses and it can dereferencethe pointers (memory addresses) so it can change the content of the variables in the main function :)
通过&变量前面的运算符,我们告诉编译器给出函数的内存地址,而不是变量本身的值。现在该函数具有地址和dereference指针(内存地址),因此它可以更改主函数中变量的内容:)

