Maximize Function Outputs: Pass by Reference Explained

function inputs and outputs n.w
1 / 12
Embed
Share

Enhance function capabilities by understanding pass by reference in programming. Explore how reference variables can efficiently handle both inputs and outputs, providing a solution to the limitations of traditional function arguments. Learn from Professor John Carelli at Kutztown University about maximizing function outputs through the power of pass by reference.

  • Functions
  • Pass by reference
  • Programming
  • Function arguments
  • Reference variables

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Function Inputs and Outputs Variables used as arguments to a function are, effectively, inputs only Why? - because arguments to functions are passed by value o Variable changes inside the function don t affect the calling arguments But can have an unlimited number of inputs Return value of a function serves as its output Only one return value - so, function is limited to one output! How can we get around these limitations? Professor John Carelli Kutztown University Computer Science Department

  2. Pass by reference A mechanism for passing unlimited amounts of information to and from a function Arguments can be made to be inputs or outputs (actually both) Makes use of reference variables Return value is retained Professor John Carelli Kutztown University Computer Science Department

  3. Reference Variable A reference variable is a variable that is linked to another, already existing, variable (i.e. it refers to the other variable) It is a separate variable with its own name, but Its shares it s data storage location with the referenced variable Can think of it as an alias for the existing variable So, changing the value of either the original or the reference variables will change the other Professor John Carelli Kutztown University Computer Science Department

  4. Reference Declaration To declare a reference variable: place & between the variable type and name int &iref = i; // iref is a reference to i float& piref= pi; // piref is a reference to pi Important notes: Reference variable must be initialized upon declaration The variable being referenced must be specified when the reference variable is created The referenced variable cannot be changed after creation The value can change, but not the variable being referenced & can be placed anywhere between the type and name Spaces don t matter next to either type or variable name Professor John Carelli Kutztown University Computer Science Department

  5. Variable storage revisited For each variable, compiler needs to keep track of 4 pieces of information: Variable name Location of data Memory address of stored data Variable type How to interpret data How many bytes? Value Stored at the memory address Memory locations ? float pi= 3.14159; 0x1000 ? Variable 0x1001 Name pi 0x1002 3.14159 Location 0x1001* 0x1003 Type float 0x1004 * float takes 4 bytes 0x1005 ? ? Professor John Carelli Kutztown University Computer Science Department

  6. Reference storage Memory locations Reference variable Original variable ? float &piref= pi; float pi= 3.14159; 0x1000 ? Variable Variable 0x1001 Name piref Name pi 0x1002 0x1001 3.14159 Location 0x1001 Location 0x1003 Type float & Type float 0x1004 Different variables with different names, but Reference uses the same storage location as the original variable 0x1005 ? ? Professor John Carelli Kutztown University Computer Science Department

  7. Functions, local variables and scope revisited All variables in a function are local to the function Including both parameters (in the input list) And variables declared in internal function statements They are created on-the-fly when the function is invoked They are destroyed when the function returns (finishes) They are automatic variables andhave local scope Any modifications to a local variable do not affect variables in the calling routine even if they have the same name! Professor John Carelli Kutztown University Computer Science Department

  8. Normal Function usage // prototype float circle1(float); // function definition // compute area of a circle float circle1(float r) { float pi= 3.14159; return pi*r*r; } main() { float area, radius=3.0; // call the function area= circle1(radius); cout << area << endl; } r and pi are local to circle1() i.e. have local scope Professor John Carelli Kutztown University Computer Science Department

  9. Example 1: pass by reference This will compute and output both the area and the perimeter of the circle! // prototype float circle1r(float, float&); // function definition // compute area and perimeter float circle1r(float r, float &p) { float pi= 3.14159; main() { float area, perim, radius= 3.0; // call the function area= circle1r(radius, perim); cout << area << endl; cout << perim << endl; } p= 2.0*pi*r; return pi*r*r; } Professor John Carelli Kutztown University Computer Science Department

  10. Example 2: pass by reference // prototype void circle2r(float, float&, float&); Don t need to use a return value! // function definition // compute area and perimeter void circle2r(float r, float &a, float &p) { float pi= 3.14159; main() { float area, perim, radius= 3.0; // call the function circle2r(radius, area, perim); cout << area << endl; cout << perim << endl; } p= 2.0*pi*r; a= pi*r*r; } Professor John Carelli Kutztown University Computer Science Department

  11. pass by reference usage // prototype float circle1r(float, float &); Both the function prototype and definition must declare parameters as reference variables Use & to identify the variable as a reference Prototype need not have variable names // definition float circle1r(float r, float &p) { float pi= 3.14159; Arguments (when the function is called ) do NOT use an & So, can t tell from a function call whether the parameters are references or not! p= 2.0*pi*r; return pi*r*r; } Professor John Carelli Kutztown University Computer Science Department

  12. Cannot use literals literals with references! Must have a variable to refer to !!! // function definition // compute area and perimeter float circle1r(float r, float &p) { float pi= 3.14159; float area, perim; // This is OK area= circle1r(3.0, perim); p= 2.0*pi*r; return pi*r*r; } // This is NOT OK!!! area= circle1r(3.0, 18.85); Professor John Carelli Kutztown University Computer Science Department

More Related Content