MCC - Marquez - CIS162AB - C++ Level I
P05b_ex An Example of Local Variables
   cpp.gif

Sample Output for P05b_ex:

P05b_ex - Juan Marquez    TR 1:00pm

Enter a value between $5 and $15.00 for the price: 5
Enter a value between 1 and 50 for the quantity: 5

main: Local values as entered before function call:
Price:          5.00
Quantity:       5

calcCost: Local values as sent to function:
Price:          5.00
Quantity:       5

calcCost: Local values modified:
Price:          10.00
Quantity:       10
Subtotal:       100.00

main: Local values after function call:
Price:          5.00
Quantity:       5
Subtotal:       100.00

Press any key to continue



Source Code:
//P05b_ex Local Variables - An Example  Juan Marquez   TR 1:00pm
/*
   This program is used to demonstrate that values cannot be returned
   through call-by-value parameters.  Any changes to the values made in
   a function are local to that function.  The values entered by the
   user are displayed before calling calcCost.  In calcCost the values
   are displayed as received, and then again after modifying them. 
   After returning to main, the local values are diplayed again to
   show that the changes made in calcCost were local and did not
   affect the variables declared in main.

   Displaying the values of variables at various points of a program is
   actually a debugging technique.  This helps programmers determine
   when a value is actually being changed.  

   Later when we get to use the debugger tool (P09) we'll see a more
   powerful way of debugging.

*/

#include <iostream>        // cin, cout
using namespace std;

//Function Prototypes
double getPrice();
int    getQuantity();
double calcCost(double price, int quantity);


void main()
{
//Declare local variables;
    int  quantity;
    double price, subtotal;

//Set the decimal point to 2 positions
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "P05b_ex - Juan Marquez    TR 1:00pm \n\n";

    price    = getPrice();
    quantity = getQuantity();

//Display values as entered before function call
    cout << endl
         << "main: Local values as entered before function call: \n"
         << "Price:    \t" << price     << endl
         << "Quantity: \t" << quantity  << endl << endl;

//Call the function which will attempt to change the values.                  
    subtotal = calcCost(price, quantity);


//Display values after function call - original values not changed.
    cout << "main: Local values after function call: \n"
         << "Price:    \t" << price     << endl
         << "Quantity: \t" << quantity  << endl
         << "Subtotal: \t" << subtotal  << endl << endl;

    return;
} // end of main


//Function Definitions

double getPrice()
{
//declare the function's local variables
    double price;
    do
    {
        cout << "Enter a value between $5 and $15.00 for the price: ";
        cin >> price;

    } while (price < 5 || price > 15);

    cout << endl;
    return (price);
}


int getQuantity()
{
    int quantity = 0;
    while (quantity < 1 || quantity > 50)
    {
        cout << "Enter a value between 1 and 50 for the quantity: ";
        cin >> quantity;
    }
    return (quantity);
}


double calcCost(double price, int quantity)
{
    double subtotal;

//Display values as sent to function
    cout << "calcCost: Local values as sent to function: \n"
         << "Price:    \t" << price     << endl
         << "Quantity: \t" << quantity  << endl << endl;

//Modify values
    quantity = 10;
    price = 10.00;

    subtotal = quantity * price;


//Display modified values before returning
    cout << "calcCost: Local values modified: \n"
         << "Price:    \t" << price     << endl
         << "Quantity: \t" << quantity  << endl
         << "Subtotal: \t" << subtotal  << endl << endl;

    return (subtotal);
}

//end of program

Revised: 08/15/2003 - www.mc.maricopa.edu/~marquez/cis162ab/p05b_ex_local_variables.html