Download presentation
Presentation is loading. Please wait.
1
Reference Parameters
2
References Reference variable Alias / shortcut to another variable
Declared with Type& Name
3
References References do not have their own value
4
References Refer to Variables
A reference MUST refer to another variable
5
Reference Parameters Reference parameter indicates pass by reference:
Standard is pass by value:
6
Reference Parameter x is a link back to num
7
Reference Parameter Uses
Another way to get information back from a function:
8
Reference Parameter Uses
Used in some library functions cin.get(char &c) get one char, possibly whitespace
9
Reference Parameter Uses
Way to get multiple pieces of information back from function:
10
Reference Parameter Efficiency
Value parameters require copying parameter What if parameter is huge? Reference parameter does not copy… links Minimal overhead Danger of modification!
11
Const References A const & can not be modified:
Any attempt is compile error:
12
Function IO Ways to give functions info
Global variables I set this random variable for you to read… Parameters Here is the list of things you asked for
13
Function IO Ways to give functions info
Global variables I set this random variable for you to read… Parameters Here is the list of things you asked for
14
Returning Information
Can get information from function with: Global variables I think you set this random variable… Reference Parameters I passed you these things by reference, you may have changed them for me… Return value You always give me back an int…
15
Returning Information
Can get information from function with: Global variables I think you set this random variable… Reference Parameters I passed you these things by reference, you may have changed them for me… Return value You always give me back an int…
16
When To Reference Function produces 1 output
return it Function needs to produce 2+ outputs extra reference parameters for outputs return void
17
When To Reference Function must modify inputs
Are you sure??? If so use refs: Function needs to work with large data item: const refs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.