4

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

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

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
redkimono
  • 43
  • 1
  • 1
  • 3

5 Answers5

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

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:

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

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

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:

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:

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

printResult(fahrenheit, rankine, celsius, kelvin);
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • wow, the pointers worked! thanks so much for helping @karlphillip, learned a lot today :) – redkimono May 18 '11 at 01:52
  • You're welcome. There's a little checkbox near my answer, you can click on it to select it as the official answer. – karlphillip May 18 '11 at 02:15
  • I understand the lesson that this homework is trying to teach, and it's a very good lesson. However as a general rule I try to avoid mutating values that are passed by reference. If you were to do this in production code that came under my review, I'd probably ask you to refactor it unless you had a very strong reason to be doing it this way (typically performance). – Ben Burns May 18 '11 at 17:52
  • I'm not saying you should never write functions that mutate their arguments, just that there doesn't seem to be a good reason for doing it here other than the lesson about pointers and pass by reference. – Ben Burns May 18 '11 at 17:53
  • 1
    I agree with you. But since this is homework I didn't wanted to write a chapter on the subject as my answer, so I omitted a few details that are not relevant at this point. I mean, the guy just learned how to implement a function with more than 1 parameter and learned how to call it properly. I thought pointers were already too much. So, baby steps! – karlphillip May 18 '11 at 18:01
  • Totally understood. This is a knock on the course material, *not* your excellent answer! – Ben Burns May 21 '11 at 22:46
2

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:

rankine = convertTemp(tempR);

Does not have 4 arguments, but your declaration does.

Mel
  • 6,077
  • 1
  • 15
  • 12
2

You must give pass in the number of arguments that a function requires. convertTemp requires 4 arguments, tempF, tempR, tempC, tempK. You're only passing in one argument in your call to 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.

Ben Burns
  • 14,978
  • 4
  • 35
  • 56
  • thanks @benburns! however according to the instructions i could only use 4 functions which includes the main function, a function that will obtain the temperature from user, one that will convert the input into rankine, celsius, and kelvin, and lastly a function that will print all converted temperatures – redkimono May 18 '11 at 01:07
2

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.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
0

You need to call convertTemp with 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.

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 dereference the pointers (memory addresses) so it can change the content of the variables in the main function :)

DipSwitch
  • 5,470
  • 2
  • 20
  • 24