1 / 31

Parameter Passing Mechanisms

Parameter Passing Mechanisms. Reference Parameters. 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. Preliminary Analysis.

adkinson
Download Presentation

Parameter Passing Mechanisms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 long distance string in ldNumber number area code string out areaCode exchange string out exchange local number string out localNum

  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> // string class #include <cctype> // 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 original 16165551234 part1 part2 part3 Memory

  14. 1. ldNumber is createdas a copy of original original 16165551234 part1 part2 part3 16165551234 ldNumber Memory

  15. 2. areaCode is createdas an alias for part1 original 16165551234 part1 areaCode part2 part3 16165551234 ldNumber Memory

  16. 3. exchange is createdas an alias for part2 original 16165551234 part1 areaCode part2 exchange part3 16165551234 ldNumber Memory

  17. 3. localNum is createdas an alias for part3 original 16165551234 part1 areaCode part2 exchange part3 localNum 16165551234 ldNumber Memory

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

  19. 5. The function computesareaCode, changing part1 original 16165551234 part1 616 areaCode part2 exchange part3 localNum 16165551234 ldNumber Memory

  20. 6. The function computesexchange, changing part2 original 16165551234 part1 616 areaCode part2 555 exchange part3 localNum 16165551234 ldNumber Memory

  21. 7. The function computeslocalNum, changing part3 original 16165551234 part1 616 areaCode part2 555 exchange part3 1234 localNum 16165551234 ldNumber Memory

  22. 8. The function returns, destroying all parameters original 16165551234 part1 616 part2 555 part3 1234 Memory

  23. 9. part1, part2, and part3now contain the information! original 16165551234 part1 616 part2 555 part3 1234 Memory

  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 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 original 16165551234 part1 part2 part3 Memory

  28. 1. ldNumber is created as aconst reference of original original 16165551234 ldNumber part1 part2 part3 Memory

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

More Related