Learn C++ - Functions Cheatsheet - Codecademy
Learn C++ - Functions Cheatsheet - Codecademy
Learn C++ - Functions Cheatsheet - Codecademy
Functions
Functions
A function is a set of statements that are executed together when the function
is called. Every function has a name, which is used to call the respective #include <iostream>
function.
// Declaring a function
void print();
int main() {
print();
}
// Defining a function
void print() {
std::cout << "Hello World!";
}
Built-in Functions
C++ has many built-in functions. In order to use them, we have to import the
required library using #include . #include <iostream>
#include <cmath>
int main() {
/
Calling a Function
In C++, when we define a function, it is not executed automatically. To execute
it, we need to “call” the function by specifying its name followed by a pair of // calling a function
parentheses () . print();
// function definition
void blah() {
std::cout << "Blah blah";
}
void Functions
In C++, if we declare the type of a function as void , it does not return a
value. These functions are useful for a set of statements that do not require #include <iostream>
returning a value.
void print() {
std::cout << "Hello World!";
}
int main() {
print();
}
/
Return Values
A function that returns a value must have a return statement. The data type
of the return value also must match the method’s declared return type; #include <iostream>
On the other hand, a void function (one that does not return anything) does
not require a return statement.
int sum(int a, int b);
int main() {
int r = sum(10, 20);
std::cout << r;
}
Parameters
Function parameters are placeholders for values passed to the function. They
act as variables inside a function. #include <iostream>
Here, x is a parameter that holds a value of 10 when it’s called.
void print(int);
int main() {
print(10);
}
void print(int x) {
std::cout << x;
}
/
Function Arguments
In C++, the values passed to a function are known as arguments. They
represent the actual input values. #include <iostream>
void print(int);
int main() {
print(10);
// the argument 10 is received as input value
}
Scope of Code
The scope is the region of code that can access or view a given element:
#include <iostream>
● Variables defined in global scope are accessible throughout the program.
● Variables defined in a function have local scope and are only accessible void print();
inside the function.
int i = 10; // global variable
int main() {
std::cout << i << "\n";
}
void print() {
int j = 0; // local variable
i = 20;
std::cout << i << "\n";
std::cout << j << "\n";
}
/
Function Declarations in Header file
C++ functions typically have two parts: declaration and definition.
Function declarations are generally stored in a header file (.hpp or .h) and // ~~~~~~ main.cpp ~~~~~~
function definitions (body of the function that defines how it is implemented)
are written in the .cpp file. #include <iostream>
#include "functions.hpp"
int main() {
// function declaration
std::string say_hi(std::string name);
#include <string>
#include "functions.hpp"
// function defintion
std::string say_hi(std::string name) {
/
Functions Definitions
In C++, it is common to store function definitions in a separate .cpp file from
the main() function. This separation results in a more efficient
implementation.
Note: If the file containing the main() function needs to be recompiled, it is
not necessary to recompile the files containing the function definitions.
Inline Functions
An inline function is a function definition, usually in a header file, qualified by
the inline keyword, which advises the compiler to insert the function’s body
where the function call is. If a modification is made in an inline function, it
would require all files containing a call to that function to be recompiled.
Default Arguments
In C++, default arguments can be added to function declarations so that it is
possible to call the function without including those arguments. If those
arguments are included the default value is overwritten. Function parameters
are read from left to right, so default parameters should be placed from right to
left.
Function Overloading
In C++, function overloading enables functions to handle different types of
input and return different types. It allows multiple definitions for the same
function name, but all of these definitions must differ in their arguments.
Function Template
A function template is a C++ tool that allows programmers to add data types as
parameters, enabling a function to behave the same with different types of
parameters. The use of function templates and template parameters is a great
C++ resource to produce cleaner code, as it prevents function duplication.