Functions Sample code

Temperature Convert

//DISPLAY 5.2 void Functions
//Program to convert a Fahrenheit temperature to a Celsius temperature.
#include <iostream>

void initialize_screen( );
//Separates current output from
//the output of the previously run program.

double celsius(double fahrenheit);
//Converts a Fahrenheit temperature
//to a Celsius temperature.

void show_results(double f_degrees, double c_degrees);
//Displays output. Assumes that c_degrees 
//Celsius is equivalent to f_degrees Fahrenheit.

int main( )
{
    using namespace std;
    double f_temperature, c_temperature;

    initialize_screen( );
    cout << "I will convert a Fahrenheit temperature"
         << " to Celsius.\n"
         << "Enter a temperature in Fahrenheit: ";
    cin >> f_temperature;

    c_temperature = celsius(f_temperature);

    show_results(f_temperature, c_temperature);
    return 0;
}

//Definition uses iostream:
void initialize_screen( )
{
    using namespace std;
    cout << endl;
    return;
}
double celsius(double fahrenheit)
{
    return ((5.0/9.0)*(fahrenheit - 32));
}
//Definition uses iostream:
void show_results(double f_degrees, double c_degrees)
{
    using namespace std;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    cout << f_degrees 
         << " degrees Fahrenheit is equivalent to\n"
         << c_degrees << " degrees Celsius.\n";
    return; // Optional
}

 

Call-by-Reference Parameters Examples

//DISPLAY 5.4 Call-by-Reference Parameters
//Program to demonstrate call-by-reference parameters.
#include <iostream>

void get_numbers(int& input1, int& input2);
//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.

void show_results(int output1, int output2);//DISPLAY 5.8 Function Calling Another Function
//Program to demonstrate a function calling another function.
#include <iostream>

void get_input(int& input1, int& input2);
//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.

void order(int& n1, int& n2);
//Orders the numbers in the variables n1 and n2
//so that after the function call n1 <= n2.

void give_results(int output1, int output2);
//Outputs the values in output1 and output2.
//Assumes that output1 <= output2


int main( )
{
    int first_num, second_num;

    get_input(first_num, second_num);
    order(first_num, second_num);
    give_results(first_num, second_num);
    return 0;
}


//Uses iostream:
void get_input(int& input1, int& input2)
{
    using namespace std;
    cout << "Enter two integers: ";
    cin >> input1 >> input2;
}

void swap_values(int& variable1, int& variable2)
{
    int temp;

    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

void order(int& n1, int& n2)
{
    if (n1 > n2)
        swap_values(n1, n2);
}

//Uses iostream:
void give_results(int output1, int output2)
{
    using namespace std;
    cout << "In increasing order the numbers are: "
         << output1 << " " << output2 << endl;
}


//Shows the values of variable1 and variable2, in that order.

int main( )
{
    int first_num, second_num;

    get_numbers(first_num, second_num);
    swap_values(first_num, second_num);
    show_results(first_num, second_num);
    return 0;
}

//Uses iostream:
void get_numbers(int& input1, int& input2)
{
    using namespace std;
    cout << "Enter two integers: ";
    cin >> input1
        >> input2;
}
void swap_values(int& variable1, int& variable2)
{
    int temp;


    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

//Uses iostream:
void show_results(int output1, int output2)
{
    using namespace std;
    cout << "In reverse order the numbers are: "
         << output1 << " " << output2 << endl;
}
 

Comparing Argument Mechanisms

//DISPLAY 5.6 Comparing Argument Mechanisms
//Illustrates the difference between a call-by-value
//parameter and a call-by-reference parameter.
#include <iostream>

void do_stuff(int par1_value, int& par2_ref);
//par1_value is a call-by-value formal parameter and
//par2_ref is a call-by-reference formal parameter.

int main( )
{
    using namespace std;
    int n1, n2;

    n1 = 1;
    n2 = 2;
    do_stuff(n1, n2);
    cout << "n1 after function call = " << n1 << endl;
    cout << "n2 after function call = " << n2 << endl;
    return 0;
}

void do_stuff(int par1_value, int& par2_ref)
{
    using namespace std;
    par1_value = 111;
    cout << "par1_value in function call = "
         << par1_value << endl;
    par2_ref = 222;
    cout << "par2_ref in function call = "
         << par2_ref << endl;
}
 

Function Calling Another Function

//DISPLAY 5.8 Function Calling Another Function
//Program to demonstrate a function calling another function.
#include <iostream>

void get_input(int& input1, int& input2);
//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.

void order(int& n1, int& n2);
//Orders the numbers in the variables n1 and n2
//so that after the function call n1 <= n2.

void give_results(int output1, int output2);
//Outputs the values in output1 and output2.
//Assumes that output1 <= output2


int main( )
{
    int first_num, second_num;

    get_input(first_num, second_num);
    order(first_num, second_num);
    give_results(first_num, second_num);
    return 0;
}


//Uses iostream:
void get_input(int& input1, int& input2)
{
    using namespace std;
    cout << "Enter two integers: ";
    cin >> input1 >> input2;
}

void swap_values(int& variable1, int& variable2)
{
    int temp;

    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

void order(int& n1, int& n2)
{
    if (n1 > n2)
        swap_values(n1, n2);
}

//Uses iostream:
void give_results(int output1, int output2)
{
    using namespace std;
    cout << "In increasing order the numbers are: "
         << output1 << " " << output2 << endl;
}


 

Time Converter

//Solution to Chapter 5 #1, Savitch 6th edition
//Bob Milnikel, 13 February 2007

#include <iostream>

using namespace std;

void convert_time(int mil_hour, int& std_hour, char& ap);
//Precondition: mil_hour is between 0 and 23.
//Postcondition: std_hour is coverted to the appropriate hour 1-12 assuming
//that mil_hour was a military hour. ap is also set to 'A' or 'P' depending
//on whether the standard hour is AM or PM.

int filter_number(int low, int high);
//Does not prompt but takes an input from the keyboard, then (if necessary)
//asks user to reenter number until it falls between low and high (inclusive).
//Returns a value between low and high.

void input_time(int& hour, int& min);
//Precondition: none
//Prompts user to enter values for the current time, in 24-hour notation.
//Postcondition: hour and min have been set to values entered by the user,
//with hour filtered to be between 0 and 23 and min between 0 and 59.

void output_time(int mil_hour, int std_hour, int min, char ap);
//Precondition: mil_hour is a number between 0 and 23, and std_hour and ap
//are set to the hour and character ('A' for am, 'P' for pm) corresponding to
//the value of mil_hour interpreted in 24-hour time.
//Postcondition: The times in both military and standard time have been sent
//to the screen nicely formatted.

char yn_filter();
//Does not prompt but takes an input from the keyboard, then (if necessary)
//asks user to reenter character until it is either 'y' or 'n'.
//Returns either 'y' or 'n' as entered by the user.


int main()
{
    int m_hour, s_hour, minutes;
    char repeat, ampm;

    cout << "This program converts 24-hour time to standard (AM/PM) time.\n\n";
    
    do{
       input_time(m_hour, minutes);
       convert_time(m_hour, s_hour, ampm);
       output_time(m_hour, s_hour, minutes, ampm);
       
       cout << "Would you like to convert another time? (y or n)   ";
       repeat = yn_filter();
       cout << endl << endl;
    }while(repeat == 'y');
    
    cout << "Thanks for converting times with me today.\n\n";

    system("PAUSE");
    return 0;
}

void convert_time(int mil_hour, int& std_hour, char& ap)
{
    if(mil_hour == 0)
    {
       std_hour = 12;
       ap = 'A';
       return;
    }
    if(mil_hour <= 11) //No need to account for 0
    {
       std_hour = mil_hour;
       ap = 'A';
       return;
    }
    if(mil_hour == 12)
    {
       std_hour = mil_hour;
       ap = 'P';
       return;
    }
    
    std_hour = mil_hour-12; //Must be between 13 and 23.
    ap = 'P';
    return;
}

int filter_number(int low, int high)
{
   int n;
   cin >> n;
   while(n < low || n > high)
   {
      cout << "Please enter a number between " << low << " and " << high << ".   ";
      cin >> n;
   }
   
   return n;
}

void input_time(int& hour, int& min)
{
    cout << "What is the hour (in 24-hour/military notation)?   ";
    hour = filter_number(0, 23);
    cout << "What is the minute?   ";
    min = filter_number(0, 59);
}

void output_time(int mil_hour, int std_hour, int min, char ap)
{
   cout << "If the time is " << mil_hour << ':'; 
   
   if(min < 10)
      cout << '0';
   
   cout << min << " in 24-hour notation,\n"
        << "then the time is " << std_hour << ':';
        
   if(min < 10)
      cout << '0';     
        
   cout << min << ' ' << ap << "M in "
        << "standard notation.\n\n";
}

char yn_filter()
{
   char yn;
   cin >> yn;
   while(yn != 'y' && yn != 'n')
   {
      cout << "Please enter either 'y' or 'n'.   ";
      cin >> yn;
   }
   
   return yn;
}



 
Topic revision: r2 - 2015-09-24 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback