Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function “Inputs and Outputs”

Similar presentations


Presentation on theme: "Function “Inputs and Outputs”"— 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” 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!

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

3 Reference Variable A reference variable is a variable that “refers” to another, already existing, variable It is a separate variable with its own name, but… It serves as an “alias“ to the existing variable So, changing the value of either the original or the reference variables will change the other Reference shares the same storage (memory) location as the original variable

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 creation References cannot be changed after creation “&” can be placed anywhere between the type and name Spaces don’t matter – next to either type or variable name

5 Variable storage 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 ? 0x1000 0x1001 0x1002 0x1003 0x1004 0x1005 float pi= ; Variable Name pi Location 0x1001 Type float

6 Reference storage float pi= 3.14159; float &piref= pi;
Memory locations ? 0x1000 0x1001 0x1002 0x1003 0x1004 0x1005 float pi= ; float &piref= pi; Variable Name pi Location 0x1001 Type float Variable Name piref Location 0x1001 Type float & Different variables with different names, but… Reference uses the same storage location as the original variable

7 Functions, local variables and “scope”
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 have local scope Any modifications to a local variable do not affect variables in the calling routine even if they have the same name!

8 “Normal” Function usage
// prototype float circle(float); main() { float area; // call the function area= circle(3.0); cout << area << endl; } // function definition / compute area of a circle float circle(float r) { float pi= ; return pi*r*r; } r and pi are local to circle() i.e. have local scope

9 Example 1: “pass by reference”
This will compute and output both the area and the perimeter of the circle! // prototype float circle(float, float&); main() { float area, perim; // call the function area= circle(3.0, perim); cout << area << endl; cout << perim << endl; } // function definition // compute area and perimeter float circle(float r, float &p) { float pi= ; p= 2.0*pi*r; return pi*r*r; }

10 Example 2: “pass by reference”
// prototype void circle(float, float&, float&); main() { float area, perim; // call the function circle(3.0, area, perim); cout << area << endl; cout << perim << endl; } Don’t need to use a return value! // function definition / compute area and perimeter void circle(float r, float &a, float &p) { float pi= ; p= 2.0*pi*r; a= pi*r*r; }

11 “pass by reference” usage
Both the function prototype and definition must declare parameters as reference variables Prototype need not have variable names Arguments (when the function is “called”) do NOT use an “&” So, can’t tell from a function call whether the parameters or references or not! Cannot use literals as arguments with a reference parameter Must have a variable to “refer to”!!!


Download ppt "Function “Inputs and Outputs”"

Similar presentations


Ads by Google