0% found this document useful (0 votes)
3 views3 pages

C++ for Programmers_ Functions in C++ Cheatsheet _ Codecademy

The document is a cheatsheet for C++ functions, covering key concepts such as function overloading, function parameters, and command line arguments. It explains how functions can have the same name with different parameters, the importance of matching argument order, and how to pass command line arguments to the main function. Additionally, it includes code examples to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

C++ for Programmers_ Functions in C++ Cheatsheet _ Codecademy

The document is a cheatsheet for C++ functions, covering key concepts such as function overloading, function parameters, and command line arguments. It explains how functions can have the same name with different parameters, the importance of matching argument order, and how to pass command line arguments to the main function. Additionally, it includes code examples to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

10/7/24, 1:42 PM C++ for Programmers: Functions in C++ Cheatsheet | Codecademy

Cheatsheets / C++ for Programmers

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 add(int a, int b, int c) {


return a + b + c;
}

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

When calling a function with multiple parameters, the #include <iostream>


number and order of the arguments must match with the
parameters.
Default parameters initialize to a default value if an double totalPrice(int items, double price
argument is not provided in the function call. = 9.99) {
Pass by reference lets the function modify the arguments
return items * price;
variables. Use the & operator to indicate that a
parameter is passed by reference. }

// 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;
}

Command Line Arguments

Command line arguments are optional arguments passed #include <iostream>


to the main() function of a C++ program.
Passing command line arguments is as easy as appending
the arguments after the executable name. For example: int main(int argc, char* argv[]) {
./greeting Hello World std::cout << argc << "\n";

In order to access command line arguments, the new


form of main() takes two arguments: for(int i = 0; i < argc; i++) {
argc : the number of command line arguments. std::cout << argv[i] << "\n";
argv : an array containing the values of command
}
line arguments.
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

A function in C++ contains a set of instructions that are #include <iostream>


executed when it is called.
A function declaration is composed of three parts:
1. Function return type void printTitle() {
2. Function name std::string msg = "Codecademy\n";
3. Function parameters
std::cout << msg;
A function can be called by specifying its name followed }
by a pair of parentheses () .

int main(){
printTitle();

return 0;
}

Print Share

https://www.codecademy.com/learn/c-plus-plus-for-programmers/modules/functions-in-cpp/cheatsheet 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy