Computer Programming/Subroutines/C++

From Wikiversity
Jump to: navigation, search

subroutines.cpp[edit]

// This program asks the user for a Fahrenheit temperature, 
// converts the given temperature to Celsius,
// and displays the results.

#include <iostream>

using namespace std;

double calculateC(double);
void displayResult(double, double);
double getF();

int main() {
    double f;
    double c;
    
    f = getF();
    c = calculateC(f);
    displayResult(f, c);
    
    return 0;
}

double getF() {
    double f;
    
    cout << "Enter Fahrenheit temperature:" << endl;
    cin >> f;
    
    return f;
}

double calculateC(double f) {
    double c;
    
    c = (f - 32) * 5 / 9;
    
    return c;
}

void displayResult(double f, double c) {
    cout << f << "° Fahrenheit is " << c << "° Celsius" << endl;
}

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own C++ compiler / interpreter / IDE.

See Also[edit]