views:

98

answers:

3
+1  Q: 

C function calling

So this program is supposed to estimate hourly temperatures throughout a day after being given the daily high, low and the hour which the low is expected. I am having problems calling up my functions inside the main function. I don't really understand how I am supposed to get specific information from the functions, and use it in place of a variable in my main function. I'm also having trouble grasping the idea of parameters and arguments. I'm sure I've messed up in more than one place here, but I'm mostly concerned about the functions right now. Any help would be appreciated.

#include <stdio.h> 
#include <math.h>

#define PI 3.14159


double getFahrTemp(double high, double low, int hour);
double fahr2cels( double fahr );
double cels2kelv( double cels );
double fahr2rank( double fahr );

double getDailyHigh()
{
  int high;
  printf("Enter the daily high temperature <F>:\n");
  scanf("%d",&high);
  return high;
}

double getDailyLow()
{
  int low;
  printf("Enter the daily low temperature <F>:\n");
  scanf("%d",&low);
  return low;
}

int getLowHour()
{
  int lowHour;
  printf("Enter the time of the daily low temperature:\n");
  scanf("%d",&lowHour);
  return lowHour;
}

double getFahrTemp(double high, double low, int hour)
{ 
  return (high-low)/2 * sin(2*PI/24 * hour + 3.0/2.0 * PI) + (high+low)/2;
}

double fahr2cels( double fahr )
{
  double cels;
  cels = fahr - 32 / 1.8;
}

double cels2kelv( double cels )
{
  double kelv;
  kelv = cels + 273;
}

double fahr2rank ( double fahr )
{
  double rank;
  rank = fahr + 459.67;
}


int main(getDailyHigh, getDailyLow, getLowHour, getFahrTemp)
{
  int hour, time;

  printf ("Temperature Scale Conversion Chart:\n")
  printf ("TIME    FAHR    CELSIUS    KELVIN    RANKINE")

  getDailyHigh();
  getDailyLow();
  getLowHour();

  do 
  {
    int time, hour=1;
    time = (hour + lowHour) % 12;

    getFahrTemp(getDailyHigh(), getDailyLow(), hour)

    fahr2cels
    cels2kelv
    fahr2rank

    printf ("%d:00   %2.2d   %2.2d   %3.2d   %3.2d\n", time, fahr, cels, kelv, rank;
    hour = hour++;
   }  
   while (hour <= 24);
}
A: 

The problem is in this section:

fahr2cels
cels2kelv
fahr2rank

You need to pass the parameter specified:

degCel = fahr2cels(degF);

Some basic rules to help you understand:

All C statements end in a semicolon (;).
(none of your functions in the block cited had a semicolon at the end...)

Look to the function definition for information on what to pass

double fahr2cels( double fahr );

Says that the function takes 1 variable, which must be a double (floating point number)

Look to the function definition for information on what the function returns
All functions return only 1 (or zero) values. But the type of that one value is important.

double fahr2cels( double fahr );

Says that the function returns a value that is a double.


Take that information together, and make some changes to your code:

double dailyHighF, dailyHighC, dailyHighK;
double dailyLowF;
int    lowHour;

dailyHighF = getDailyHigh();
dailyLowF = getDailyLow();
lowHour = getLowHour();

dailyHighC = farh2cels(dailyHighF);
dailyHighK = cels2kelv(dailyHighC);

Another thing to note: your functions are declared to return double, but they declare, scanf, and return ints. Its not a huge problem as the integers will get automatically changed into doubles. But you will be much better off if you are consistent in your types. If the function will return a double, it should have a double variable inside it.

abelenky
Ok, that was what I was looking for. I didn't know how to tell the function to take a certain variable and use it in the function. Then store the result as a variable in the main function. Now I am getting errors saying that they are not functions. What else could I be missing? ^ the fahr2cels, cels2kelv, and fahr2rank functions.
Jesse
"error: called object ‘getDailyHigh’ is not a function"
Jesse
You'll have to post your code before I know exactly what the error is. Please edit your original code to show the updates you've made. Then cite the exact error message (including line number).
abelenky
Where main starts, you have: main(getDailyHigh, getDailyLow, getLowHour, getFahrTemp). As far as a beginner knows (for now), main does not have any parameters, and should be written as main(void). When you get more advanced, you'll learn what the parameters to main are, and how to use them.
abelenky
A: 

e.g.

double fahr2cels( double fahr );
...

double fahr = 101.0;
double celsius = fahr2cels( fahr );

the return statment in the function returns the value to the caller.

Anders K.
+2  A: 

I don't really understand how I am supposed to get specific information from the functions, and use it in place of a variable in my main function. I'm also having trouble grasping the idea of parameters and arguments.

Do you understand the concept of a function in mathematics? For example, the equation to convert celcius to fahrenheit is:

°C = (°F − 32) × 5⁄9

One can write this as a mathematical function:

f(x) = (x - 32) × 5⁄9

The function f accepts a single argument called x and returns (x - 32) × 5⁄9. To "use" the function, you would write:

y = f(x)

Given a variable x, you can assign the result of function f to a variable y.

Once you understand this, you can easily see how it transfers to programming:

double fahr2cels(double f)
{
    return (f - 32) * 5 / 9;
}

Calling the function even looks like "how math is done":

double celcius = fahr2cels(fahrenheit);

In the same way you can have multivariable functions in math, you can have functions that accept multiple parameters. You can even have functions that accept no parameters!

double getFahrTemp(double high, double low, int hour)  
{   
    return (high-low)/2 * sin(2*PI/24 * hour + 3.0/2.0 * PI) + (high+low)/2;  
}

The syntax for calling a function is fairly consistent:

// Call the function getFahrTemp(), passing three parameters.
// The variable fahrtemp receives the result of the function call.
double fahrtemp = getFahrTemp(high, low, hour);

There are some important differences I must take note of in this math analogy - functions in C can have side effects (they affect program state in some way outside the function). Also, parameters that you pass are always copied.

In silico