Title: Functions with Reference Parameters
1Chapter 9
Functions with Reference Parameters
2Outline
- Reference parameters in input functions
- General passing by reference
- const reference parameters
- Review of global scope
- Hand Tracing
3Value parameters passing by value
- What were doing so far is passing by value
- A copy of that arguments value is passed to the
corresponding parameter of the called function - If a function changes the value of one of its
parameters, that change will stay local to the
function - it will NOT affect the arguments value back at
the point of the call
void inc(int x) int main () int a 5
inc(a) cout ltlt a return 0 void inc (int
x) x
void inc(int a) int main () int a 5
inc(a) cout ltlt a return 0 void inc (int
a) a
4Reference parameters Passing by Reference
- A reference parameter allows a function call to
give a value to or update the current value of a
variable argument appearing in the call. - The ampersand, , between the parameters data
type and its name, says the parameter is a
reference parameter
void inc(int x) int main () int a 5
inc(a) cout ltlt a return 0 void inc (int
x) x
The following notations are equivalent int
x int x int x
5Reference parameters Passing by Reference
- void input(double dWeight, char chConfirm)
- int main ()
-
- double weight
- char confirm
-
- input(weight, confirm)
- cout ltlt weight ltlt ltlt confirm
- return 0
-
-
- void input(double dWeight, char chConfirm)
-
- cout ltlt "Please enter the weight "
- cin gtgt dWeight
- cout ltlt Delivery confirmation (y or n)? "
1. Use an ampersand between the type name and
the variable name ...
2. In the parameter list of both the function
declaration and the header of the functions
definition
3. NO (and data type) in function call.
6Reference parameters Passing by Reference
7Reference parameters Passing by Reference
8Reference parameters Passing by Reference
9Argument-to-Parameter Matching
- Value Parameters
- Argument can be an expression, variable, or
literal - C will do its best to match data types
- Reference Parameters
- Argument must be a variable
- Not a literal literals values cant be changed
- Argument and parameter must be of same data type
- One exception, for file types chapter 11
- If function is to reach back to the variable at
point of call, it must know that variables data
type exactly - As before, argument/parameter matching is by
order names are totally irrelevant
10Argument-to-Parameter Matching
Argument must be a variable
11Argument-to-Parameter Matching
Argument and parameter must be of same data type
12Use Reference Parameters in Functions that
calculate more than one value
- Value-returning function usually only return one
value
- So lets return the results in Reference
Parameters
Area
Circum
- A value-returning function should be used only
when the sole purpose is to calculate and return
a single value. - A value-returning function can be used with a
reference parameter
13One more example swapping two values
14One more example swapping two values
if (w2 gt w3) swap(w2, w3)
15const Reference Parameters
16Constant Parameters
- The const modifier can also be applied
to reference parameters - const says that the function can not modify
the parameters value, even though its by
reference - Sample call in main program
- int Num1
- Get_Int ("Enter integer number ", Num1)
- function definition
- void Get_Int (const string Prompt, int Nbr)
- cout ltlt Prompt
- cin gtgt Nbr
- Prompt "Got it"
- cout ltlt Prompt
Illegal assignment
17Constant Parameters
- When do we use them?
- When we need to pass a variable (object) by
reference, but do not want to let the called
function modify it - Question
- Then why not just pass the variable (object) by
value? - Answer
- Yes, usually we should pass it by value
- However, for large, complex objects, making a
copy of the whole object can be very inefficient,
in terms ofstorage space and/or time - Array, by default, is always passed by reference
18A Tale of Two Functions
- This code will produce a compiler error
- void Func (string S)
- . . . . . . . . . . . . . . .
- int main()
-
- Func ("This is a sample string.")
- return 0
-
- . . . . . . . . . . . . . . .
- void Func (string S)
-
- cout ltlt S ltlt endl
19A Tale of Two Functions
20A Tale of Two Functions, contd.
- But this code will compile just fine
- void Func (const string S)
- . . . . . . . . . . . . . . .
- int main()
-
- Func ("This is a sample string.")
- return 0
-
- . . . . . . . . . . . . . . .
- void Func (const string S)
-
- cout ltlt S ltlt endl
(Constant argument requires a const reference
parameter)
21Global Scope
22Global Scope
- An constant or variable is global if it
is available to all functions of the program - Defined before and outside any function
- Example
- // Joes second program 04/07/1998
- include ltiostreamgt
- using namespace std
- const double PI 3.1415926535
- bool Debugging false
- int main ()
-
- ...
Global items
23Global Scope
- Global Constants
- DO use them
- Two main situations
- Physical or mathematical constants (PI)
- Data structure sizes used several places in the
code - Global Variables
- Do NOT use them
- Except in really exceptional circumstances
- Puts you in danger of unexpected side-effects
- Function does its job, but also changes a global
variable - Some other function doesnt expect that variable
to change