Hello,
I am learning about functions and encapsulation. I have pasted the program example below the message and would like to understand how the function's argument "tempCelsius" is passed onto the function and most of all why we need to define the parameter "tempCels" which uses the argument's value. What is the difference between the two and why in the function definition (green)we cant just write:
double celsiusToFahrenheit(double tempCelsius)
as defined in red previously? What is the need to define "tempCels" and how does the function definition know to take the value of "tempCelsius and place it into tempCels"?
Thanks in advance
Best
Code:
/* This program converts temperatures from Celsius to Fahrenheit,
using the standard Celsius-to-Fahrenheit conversion
formula.
Entering any value less than -273.15 Celsius (approx. absolute
zero) terminates the program.
Input: tempCelsius
Output: tempFahrenheit
--------------------------------------------------------------------*/
#include <iostream>
using namespace std;
double celsiusToFahrenheit(double tempCels); // function prototype
int main()
{
cout << "Program to convert Celsius temperatures to Fahrenheit.\n";
double tempCelsius, // Celsius temperature
tempFahrenheit; // Fahrenheit temperature
cout <<"\nEnter a Celsius temperature (< -273.15 to stop): ";
cin >> tempCelsius;
while (tempCelsius >= -273.15)
{
tempFahrenheit = celsiusToFahrenheit(tempCelsius);
cout << tempCelsius << " degrees Celsius is equivalent to "
<< tempFahrenheit << " degrees Fahrenheit\n";
cout <<"\nEnter a Celsius temperature (< -273.15 to stop): ";
cin >> tempCelsius;
}
}
double celsiusToFahrenheit(double tempCels) // function definition
{
return 1.8 * tempCels + 32;
}