Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter Passing Mechanisms Reference Parameters.

Similar presentations


Presentation on theme: "Parameter Passing Mechanisms Reference Parameters."— Presentation transcript:

1 Parameter Passing Mechanisms Reference Parameters

2 Problem 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.

3 Preliminary 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 ;

4 Behavior 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.

5 Objects Description Type Movement Name area code string out areaCode exchange string out exchange local number string out localNum long distance string in ldNumber number

6 Operations 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 ??

7 Algorithm 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.

8 Discussion 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.

9 Solution Reference parameters are parameters declared with an ampersand (&) following the parameter’s 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.

10 Coding #include // string class #include // 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 < ldNumber.size(); i++) // check all assert(isdigit(ldNumber[i])); // digits assert(ldNumber[0] == ‘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); }

11 Testing The caller must now supply a variable for each reference parameter, to be “filled in” by the function. cout << “Enter a L-D phone number: “; string original, part1, part2, part3; cin >> original; ChopLDNumber(original, part1, part2, part3); cout << “\nArea code: “ << part1 << “\nExchange: “ << part2 << “\nLocal number: “ << part3 << endl;

12 Notes 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.

13 0. Before the function call Memory original part1 part2 part

14 1. ldNumber is created as a copy of original Memory original part1 part2 part ldNumber

15 2. areaCode is created as an alias for part1 Memory original part1 part2 part ldNumber areaCode

16 3. exchange is created as an alias for part2 Memory original part1 part2 part ldNumber areaCode exchange

17 3. localNum is created as an alias for part3 Memory original part1 part2 part ldNumber areaCode exchange localNum

18 4. The function checks ldNumber for validity Memory original part1 part2 part ldNumber areaCode exchange localNum

19 5. The function computes areaCode, changing part1 Memory original part1 part2 part ldNumber areaCode exchange localNum 616

20 6. The function computes exchange, changing part2 Memory original part1 part2 part ldNumber areaCode exchange localNum

21 7. The function computes localNum, changing part3 Memory original part1 part2 part ldNumber areaCode exchange localNum

22 8. The function returns, destroying all parameters Memory original part1 part2 part

23 9. part1, part2, and part3 now contain the information! Memory original part1 part2 part

24 Notes By default, parameters are value parameters. Reference parameters are specified by placing an ampersand after the parameter’s type. Reference parameters must be specified in both a function’s 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.

25 Consider 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.

26 Solution Constant reference parameters are reference parameters whose declaration is preceded by the keyword. 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.

27 0. Before the function call Memory original part1 part2 part

28 1. ldNumber is created as a const reference of original Memory original part1 part2 part ldNumber

29 Conclusion 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).

30 Discussion 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.

31 Summary 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.

32 Announcement Lab Test: Open books, Open notes.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.


Download ppt "Parameter Passing Mechanisms Reference Parameters."

Similar presentations


Ads by Google