ITP 165x Lecture Notes - Lecture 11: Function Overloading

26 views1 pages
ITIP 165: Intro to C++ - Lecture 11: More Functions, Passing by Reference
Stacking Papers Analogy
1. The desk is empty right before the program starts
2. When a function starts, its paper gets put on top of the paper stack
3. The “current” function is the paper on top of the stack
4. When a function ends, its paper is removed, and the program continues on the paper
below it
5. If there are no papers left on the desk, the program is over
Pass by Reference
If you want to swap values, you must pass by reference
If we pass a parameter by reference, modifications to the parameter will persist
To pass a parameter by reference, in the function declaration add a & between the type
and variable name
When calling a function with pass-by-reference parameters, the parameters MUST BE
variables
If you pass in a literal number (e.g. 10), C++ will recognize it as a constant
Constants cannot be updated or modified
When passing by reference, no copy is made, saves memory
When to Pass by Reference
For basic types (such as int, char, float, double, and so on):
Usually, pass by value
…UNLESS you do want parameter modifications to persist (as is the case with
swap)
For any non-basic types (everything other than basic types including std::string,
std::ifstream, std::ofstream, etc.):
Usually, pass by reference
…UNLESS you need to modify the parameter in the function and don’t want
modifications to persist
Function Overloading
Function overloading is when you declare two functions with the same name, but they
take in different parameter types
compile error, because it doesn’t know which version to pick!
You can’t overload a function just by changing the return type. You have to also change
parameter types
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Itip 165: intro to c++ - lecture 11: more functions, passing by reference. The desk is empty right before the program starts. When a function starts, its paper gets put on top of the paper stack. The current function is the paper on top of the stack. When a function ends, its paper is removed, and the program continues on the paper below it. If there are no papers left on the desk, the program is over. If you want to swap values, you must pass by reference. If we pass a parameter by reference, modifications to the parameter will persist. To pass a parameter by reference, in the function declaration add a & between the type and variable name. When calling a function with pass-by-reference parameters, the parameters must be variables. If you pass in a literal number (e. g. 10), c++ will recognize it as a constant. When passing by reference, no copy is made, saves memory.

Get access

Grade+20% OFF
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers