Title: Our program should receive from its caller a long-distanc
1Parameter Passing Mechanisms
Reference Parameters
2Problem
- Using OCD, design and implement a function that,
given a string containing a long-distance
telephone number, decomposes that number into its
area code, exchange, and local number.
3Preliminary Analysis
- Our function can receive the long-distance phone
number through a string parameter. - This problem requires that our function somehow
communicate three values (area code, exchange,
local number) to its caller. - A function cannot return multiple values -- the
return statement only returns one value - return Expression
4Behavior
- Our program should receive from its caller a
long-distance phone number (a string). It
should check that the number is a valid long-
distance number. If so, it should compute and
pass back its area code, exchange, and local
number.
5Objects
- Description Type Movement Name
long distance string in ldNumber
number
area code string out areaCode
exchange string out exchange
local number string out localNum
6Operations
- Description Predefined? Library?
Name
receive a string yes built-in --
select part of yes string substr a
string
pass back 3 strings yes built-in ??
7Algorithm
- 0. Receive ldNumber from caller, plus empty
variables areaCode, exchange and localNum. - 1. Check that ldNumber is a long-distance number.
- 2. Fill areaCode with appropriate substring of
ldNumber. - 3. Fill exchange with appropriate substring of
ldNumber. - 4. Fill localNum with appropriate substring of
ldNumber.
8Discussion
- Since a function cannot return 3 strings, we
will instead require the caller to pass us three
string variables, which our function will then
fill in with the appropriate values. - Normal parameters are called value parameters and
are built as copies of their arguments. - Changing a value parameter changes the copy, not
its corresponding argument.
9Solution
- Reference parameters are parameters declared with
an ampersand () following the parameters type
(and before its name). - A reference parameter is an alias (i.e., another
name for) its corresponding argument. - Changing the value of a reference parameter
changes the value of its corresponding argument.
10Coding
- include ltstringgt // string
class - include ltcctypegt // isdigit()
- using namespace std
- void ChopLDPhoneNumber(string ldNumber, //
value IN - string areaCode, //
reference OUT - string exchange, //
reference OUT - string localNum) //
reference OUT -
- for (int i 0 i lt ldNumber.size() i)
// check all - assert(isdigit(ldNumberi))
// digits - assert(ldNumber0 1 // check for
leading 1 - ldNumber.size() 11) // check
number of digits - areaCode ldNumber.substr(1, 3)
- exchange ldNumber.substr(4, 3)
- localNum ldNumber.substr(7, 4)
11Testing
- The caller must now supply a variable for each
reference parameter, to be filled in by the
function. - cout ltlt Enter a L-D phone number
- string original, part1, part2, part3
- cin gtgt original
- ChopLDNumber(original, part1, part2, part3)
- cout ltlt \nArea code ltlt part1
- ltlt \nExchange ltlt part2
- ltlt \nLocal number ltlt part3
- ltlt endl
12Notes
- When function ChopLDNumber() is called
- a copy of argument original is made to create
parameter ldNumber, - an alias of argument part1 is made to create
parameter areaCode, - an alias of argument part2 is made to create
parameter exchange, - an alias of argument part3 is made to create
parameter localNum.
130. Before the function call
original
16165551234
part1
part2
part3
Memory
141. ldNumber is createdas a copy of original
original
16165551234
part1
part2
part3
ldNumber
16165551234
Memory
152. areaCode is createdas an alias for part1
original
16165551234
part1
areaCode
part2
part3
ldNumber
16165551234
Memory
163. exchange is createdas an alias for part2
original
16165551234
part1
areaCode
part2
exchange
part3
ldNumber
16165551234
Memory
173. localNum is createdas an alias for part3
original
16165551234
part1
areaCode
part2
exchange
part3
localNum
ldNumber
16165551234
Memory
184. The function checks ldNumber for validity
original
16165551234
part1
areaCode
part2
exchange
part3
localNum
ldNumber
16165551234
Memory
195. The function computesareaCode, changing part1
original
16165551234
part1
areaCode
616
part2
exchange
part3
localNum
ldNumber
16165551234
Memory
206. The function computesexchange, changing part2
original
16165551234
part1
areaCode
616
part2
exchange
555
part3
localNum
ldNumber
16165551234
Memory
217. The function computeslocalNum, changing part3
original
16165551234
part1
areaCode
616
part2
exchange
555
part3
localNum
1234
ldNumber
16165551234
Memory
228. The function returns, destroying all parameters
original
16165551234
part1
616
part2
555
part3
1234
Memory
239. part1, part2, and part3now contain the
information!
original
16165551234
part1
616
part2
555
part3
1234
Memory
24Notes
- By default, parameters are value parameters.
- Reference parameters are specified by placing an
ampersand after the parameters type. - Reference parameters must be specified in both a
functions prototype and its definition, or a
linking error will occur. - Variables must be passed as arguments for
reference parameters to fill, or a compiler error
will occur.
25Consider
- Copying argument original consumes time.
- Creating an alias for an argument takes almost no
time. - We could speed up calls to our function by making
parameter ldNumber a reference parameter. - However, we then run the risk of changing
original if we mistakenly change ldNumber.
26Solution
- Constant reference parameters are reference
parameters whose declaration is preceded by the
keyword const.
void ChopLDPhoneNumber(const string ldNumber,
// IN string areaCode,
// OUT string
exchange, // OUT
string localNum) // OUT // ...
Const reference parameters are read-only
reference parameters -- aliases of their
arguments -- but they cannot be changed.
270. Before the function call
original
16165551234
part1
part2
part3
Memory
281. ldNumber is created as aconst reference of
original
original
16165551234
ldNumber
part1
part2
part3
Memory
29Conclusion
- The rest of the function proceeds as before,
except that all accesses to ldNumber now access
original instead of the copy. - Any attempt to change ldNumber will generate a
compiler error (which makes sense, since its
movement is IN, not OUT).
30Discussion
- Copying time is not significant for simple types
(e.g., int, char, double, ...), but it is
significant for class types (e.g., string,
RandomInt, ...). - Use reference parameters for arguments whose
movement is OUT. - Use const reference parameters to store class
arguments whose movement is IN. - Use value parameters to store simple type
arguments whose movement is IN.
31Summary
- C provides 3 parameter mechanisms
- value, used for IN parameters whose arguments are
simple types. - const reference, for IN parameters whose
arguments are class types. - reference, for all OUT parameters.
32Announcement
- Lab Test Open books, Open notes.
- Wed. 2 hours.
- 3 programs.
- You need prepare your money on your card.
- You need to print all the results out in the lab.
- You need in hand in the test sheet.