C++ for Programmers_ Functions in C++ Cheatsheet _ Codecademy
C++ for Programmers_ Functions in C++ Cheatsheet _ Codecademy
Functions in C++
Function Overloading
With function overloading, C++ functions can have the #include <iostream>
same name but handle different input parameters.
At least one of the following criteria must be true in order
for functions to be properly overloaded: int add(int a, int b) {
Each function has different types of parameters. return a + b;
Each function has a different number of
}
parameters.
The function return type is NOT used to differentiate
overloaded functions. double add(double a, double b) {
return a + b;
}
int main() {
std::cout << add(3, 2); // Calls
add(int, int)
std::cout << "\n";
std::cout << add(5.3, 1.4); // Calls
add(double, double)
std::cout << "\n";
std::cout << add(2, 6, 9); // Calls
add(int, int, int)
}
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/functions-in-cpp/cheatsheet 1/3
10/7/24, 1:42 PM C++ for Programmers: Functions in C++ Cheatsheet | Codecademy
Function Parameters
// Pass by reference
void addOne(int &i) {
i += 1;
}
int main() {
std::cout << totalPrice(10) << "\n"; //
Output: 99.9
int num = 2;
addOne(num);
std::cout << num; // Output: 3
return 0;
}
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/functions-in-cpp/cheatsheet 2/3
10/7/24, 1:42 PM C++ for Programmers: Functions in C++ Cheatsheet | Codecademy
Introduction to Functions
int main(){
printTitle();
return 0;
}
Print Share
https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/functions-in-cpp/cheatsheet 3/3