FunctionX - Practical Learning Logo

Passing Arguments by Value

This illustrates the technique used to pass arguments by value to a function.

#include <iostream>
#include <string>
using namespace std;

string GetName()
{
    string FirstName, LastName, FN;   

    cout << "Employee's First Name: ";
    cin >> FirstName;
    cout << "Employee's Last Name: ";
    cin >> LastName;  

    FN = FirstName + " " + LastName;

    return FN;
}

int main()
{
    string FullName;
    double Hours;

    double GetHours(string FullName);

    FullName = GetName();

    Hours = GetHours(FullName);

    cout << "\nEmployee's Name: " << FullName;
    cout << "\nWeekly Hours: " << Hours << " hours\n\n";

    return 0;
}

double GetHours(string FullName)
{
    double Mon, Tue, Wed, Thu, Fri, TotalHours;   

    cout << endl << FullName << "'s Weekly Hours\n";
    cout << "Monday: ";
    cin >> Mon;
    cout << "Tuesday: ";
    cin >> Tue;
    cout << "Wednesday: ";
    cin >> Wed;
    cout << "Thursday: ";
    cin >> Thu;
    cout << "Friday: ";
    cin >> Fri;  

    TotalHours = Mon + Tue + Wed + Thu + Fri;

    return TotalHours;
}

Copyright 2003-2009 FunctionX, Inc.