So So - 4 months ago 12
C Question

# Understanding functions and parameters in C

I created a function fahrenheitFromCelsius but I don't quite get everything starting with line 1. Can someone please explain it to me, especially

`(float cel)`
. Also on line 2, I want to know the purpose for this line, and in line 3, I want to know what is going on too. Please explain in a simple way from top to bottom and also explain how this determined my output.

``````   #include <stdio.h>

1)  float fahrenheitFromCelsius(float cel)
{
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

2)  int main(int argc, const char * argv[]) {
float freezeInC = 0;
3)    float freezeInF = fahrenheitFromCelsius(freezeInC);
printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);
return 0;
}
``````

Output:

``````0.000000 Celsius is 32.000000 Fahrenheit
Water freezes at 32.000000 degrees Fahrenheit
``````

1. This is the definition of the function named `fahrenheitFromCelsius`. It accepts a `float` value and returns a `float` value. Inside the function, it refers to its argument by the name `cel` because that was what it was declared to be.
2. This is the definition of the function `main`. Every C program needs one. It basically marks the starting point of the entire program. This function gets executed first automatically. It takes as arguments an `int` named `argc` which denotes the number of command line arguments supplied to the program. The second argument is an array of `const char *`s named `argv`. This contains the individual command line arguments supplied to the program. They don't play any part in this specific program however, so you can just ignore them in this case. (Indeed, it would be better to use `int main(void)` to explicitly ignore the arguments to `main`.)
3. This line of code declares a variable named `freezeInF` of type `float`. It takes the value returned by the function `fahrenheitFromCelsius` and passes it the argument `freezeInC`. In simple terms, it converts the freezing point from Celsius to Fahrenheit and stores it in `freezeInF`.