1 / 12

Pass-By-Reference

03/16/11. Pass-By-Reference. Getting Info Back from a Function. return can only return one value back to a calling function. Sometimes you want to pass more than one value. Example. Want to scale a rectangle in graphic. One foot is scaled to 1/4 inch.

jock
Download Presentation

Pass-By-Reference

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. 03/16/11 Pass-By-Reference

  2. Getting Info Back from a Function • returncan only return one value back to a calling function. • Sometimes you want to pass more than one value.

  3. Example • Want to scale a rectangle in graphic. • One foot is scaled to 1/4 inch. • I want a new measure for both length and width. • Must transmit more than one value by to calling program. • scale.cpp

  4. Pass-By-Reference • Syntax void scale(double& ln, double& wd); • & indicates pass-by-reference parameter

  5. Pass-By-Reference • Call: scale(length, width); • Function Heading: void scale(double& ln, double& wd) • ln and wd contain address of length and width • Changes in the parameters affect arguments, length and width.

  6. Stock Function • Write a function that takes the value of a stock today and its value a year ago. It should give the difference in price and the percent change. • e.g. Apple $305.97 Thursday, $225.50 one year ago.

  7. Movie Time Function Write a Function, movieTime( ), that has an integer parameter named minutes and two integer parameters named hours and min. The function is to convert the passed number of minutes a movie lasts into an equivalent number of hours and minutes. Using pass by reference the function should directly alter the respective arguments of the calling function.

  8. Which to Use • Pass-by-reference • Want a change in the associated argument • Pass-by-value • No change wanted in argument • fig5_13.cpp has both

  9. Functions to Improve Programs • Break up into parts • Easier to read • Debug and Test one piece at a time • RPS.cpp

  10. Use Right Kind of Function • One value to return • Function that returns a single value • e.g. double area(double l, double w); • No value to return • void function • e.g. void instructions();

  11. Use Right Kind of Function • More one value to return • void function • Reference parameters • e.g. void scale(double &l, double &w);

  12. To Do • p. 179 #1 & 3 • Read pp. 180-188 • Skipping Recursion at the end of Ch. 5 • A function calling itself. • Generally avoid that. • Can make some problems simpler

More Related