Function Parameters

In many books and conversations, the terms parameter and argument are used interchangably. The term formal parameter is also used in discussions about functions. For this discussion,

The two basic kinds of parameters are "call-by-value" parameters and "call by reference" parameters.

Call-by-value parameters

Call-by-value parameters are the kind you've been using so far in all your programming. The call-by-value parameter is a placeholder into which the value of the corresponding argument is copied. Note that the corresponding argument may be a variable or an expression. As the example code from the text below shows, the call-by-value parameter is actually a local variable and may be changed within the function without affecting the corresponding argument in the calling code. //--------------------------------- // File: 04-01.cpp // Date: 8/20/03 // Author: D. Frey // Section: n/a // EMail: frey@cs.umbc.edu // // This is a modified version of example 04-01 // from the text, page 135 //---------------------------- #include <iostream> using namespace std; // prototypes for functions in this file double fee(int hoursWorked, int minutesWorked); int main ( ) { int hours, minutes; double bill; cout << "Welcome to the law office of\n" << "Dewey, Cheatham, and Howe.\n" << "The law office with a heart.\n" << "Enter the hours and minutes" << " of your consultation:\n"; cin >> hours >> minutes; bill = fee(hours, minutes); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For " << hours << " hours and " << minutes << " minutes, your bill is $" << bill << endl; return 0; } //-------------------------------------------------- // Function: fee( ) // PreConditions: // minutesWorked and hoursWorked should be nonnegative // PostConditions: // Returns the charges for hoursWorked hours and // minutesWorked minutes of legal services. Returns // zero if either parameter is negative //--------------------------------------------------- double fee (int hoursWorked, int minutesWorked) { if (hoursWorked < 0 || minutesWorked < 0) return 0.0; const double RATE = 150.00; //Dollars per quarter hour. int quarterHours; minutesWorked = hoursWorked * 60 + minutesWorked; quarterHours = minutesWorked / 15; return (quarterHours * RATE); }


Last Modified: Monday, 28-Aug-2006 10:15:59 EDT