Thread: Functions and Encapsulation

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    2

    Functions and Encapsulation

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to format your program properly by the use of appropriate indentation and other whitespace, e.g.,
    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;
    }
    Quote Originally Posted by Alienspecimen
    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
    The actual argument is the object that is passed to the function call at the place of the call. The formal parameter is the variable is that declared to correspond to the various actual arguments, being in the scope of the function's body rather than in the scope of the place of the call.

    Quote Originally Posted by Alienspecimen
    why in the function definition (green)we cant just write:

    double celsiusToFahrenheit(double tempCelsius)

    as defined in red previously?
    It looks like you have a serious typo error in your question that makes it hard to understand because as written, your question does not make sense.

    Quote Originally Posted by Alienspecimen
    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"?
    You need to declare tempCels so that the function has a parameter such that at the call site, a value can be passed to the function that will correspond to this parameter. The function definition "knows" that the argument corresponds to the parameter by position, i.e., the first argument in the function call corresponds to the first parameter in the function definition, the second argument corresponds to the second parameter, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with encapsulation
    By Jefff in forum C++ Programming
    Replies: 6
    Last Post: 06-13-2012, 08:23 AM
  2. Encapsulation Vs Abstraction
    By Rakesh Kp in forum C++ Programming
    Replies: 1
    Last Post: 09-13-2011, 02:30 AM
  3. Encapsulation in C++
    By JohnLeeroy in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2011, 05:02 PM
  4. encapsulation in c
    By swaugh in forum C Programming
    Replies: 2
    Last Post: 12-01-2004, 01:37 PM
  5. Integration or Encapsulation :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 06-02-2002, 09:14 AM

Tags for this Thread