参数太少起作用,并且不能用作函数----开始Ç [英] too few arguments to function and can't be used as a function---- beginning C

查看:232
本文介绍了参数太少起作用,并且不能用作函数----开始Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是一个初学者,我有这个功课我开始C级。我不断收到错误,因为我特别我的函数编写的程序。下面是我的程序:

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:

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

对不起,我是初学者...我真的AP preciate你的帮助:)

Sorry, I am a beginner...i would really appreciate your help :)

推荐答案

错误是pretty清楚,你不调用该函数它应该是的方式。该函数接受4个参数,而你只有经过之一。

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.

不过,这只是你的第一个错误。第二,是函数的参数,因为他们宣称,现在,将使参数的本地副本:

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);

这意味着,函数体内部,对这些变量的变化将不会传播到你用来调用主声明的变量 convertTemp()。我想说的是在函数被调用的时候,是在栈上创建的另外4个变量和它们的值是从您发送给函数的变量复制。

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.

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

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:

无效convertTemp(双* tempF,双* tempR,双* tempC,双* tempK);

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

请注意,新函数签名不返回任何值(即无效的)。这是没有必要的,因为你会被由传递的变量的main()

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().

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

On main(), you should call the function like:

fahrenheit = obtainTemp();
convertTemp(&fahrenheit, &rankine, &celsius, &kelvin);


  • 第二种方法,因为你是一个初学者,这可能将是您更容易理解,就是申报3个功能,每一个你需要做的转换:

    • 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:
    • 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(),你会打电话给他们这样的:

      Then on main(), you would call them like:

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

      这篇关于参数太少起作用,并且不能用作函数----开始Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆