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

时间:2011-05-18 00:42:42

标签: c function

嗨,我是初学者,我有这个功课为我的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;
}

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

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

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

5 个答案:

答案 0 :(得分:4)

错误非常清楚,你不是按照预期的方式调用函数。该函数有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);

答案 1 :(得分:2)

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

rankine = convertTemp(tempR);

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

答案 2 :(得分:2)

您必须传递函数所需的参数数量。 convertTemp需要4个参数,tempFtempRtempCtempK。您在convertTemp的电话中只传递了一个参数。

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

答案 3 :(得分:2)

  

下面这个功能太少了   参数,

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

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

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

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

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

#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;
}

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

答案 4 :(得分:0)

您需要使用主要功能中的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指针(内存地址),所以它可以改变main函数中变量的内容:)