FUNCTION
S IN C++
C++
Language
● A function
C++ Functions
is a block of Functions are
code which used to
only runs perform
when it is certain
called. actions, and
● You can they are
pass data, important for
known as reusing code:
Define the
parameters,
code once,
into a
and use it
function.
many times.
Create a Function
● C++ provides some predefined functions,
such as main(), which is used to execute
code. But you can also create your own
functions to perform certain actions.
● To create (often referred to as declare) a
function, specify the name of the function,
followed by parentheses ():
Syntax:
void myFunction()
{
// code to be
executed
}
Call a Function
To call a function, write the function's name followed by two parentheses () and
a semicolon ;
In the following example, myFunction() is used to print a text (the action), when
Example:
it is called:
#include <iostream>
using namespace std;
void myFunction() {
cout << "I just got
executed!";
}
int main() {
myFunction();
return
Multiple calling of a
function
#include <iostream>
using namespace std;
void myFunction() {
cout << "I just got executed!\
n";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
Function Declaration and
Definition
A C++ function consist of two parts:
● Declaration: the function's name, return
type, and parameters (if any)
● Definition: the body of the function (code
to be executed)
void myFunction() { // declaration
// the body of the function
(definition)
}
Note: If a user-defined function, such as
myFunction() is declared after the main()
function, an error will occur:
Example:
#include <iostream>
using namespace std;
// Function declaration
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
cout << "I just got executed!";
}
Parameters and Arguments
Information can be
passed to functions
as a parameter.
Parameters act as
variables inside the Syntax
function.
Parameters are void
specified after the functionName(paramet
er1, parameter2,
function name,
parameter3)
inside the {
parentheses. You can // code to be executed
add as many }
parameters as you
want, just separate
them with a comma.
Example:
#include <iostream>
#include <string>
using namespace std;
void myFunction(string fname) {
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
Default Parameter
Value
You can also use a default parameter value, by using the
equals sign (=).
If we call the function without an argument, it uses the
default value ("Norway"):
Example
#include <iostream>
#include <string>
using namespace std;
void myFunction(string country = "Norway") {
cout << country << "\\n";
}
int main()
{
myFunction("Sweden");
myFunction("India");
myFunction();
Multiple Parameters
Inside the function, you can add as many parameters as you want.
Example:
#include <iostream>
#include <string>
using namespace std;
void myFunction(string fname, int age) {
cout << fname << " Refsnes. " << age << " years
old. \n";
}
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
Return Values
The void keyword, used in
the previous examples,
indicates that the function
should not return a value.
If you want the function to
return a value, you can
use a data type (such as
int, string, etc.) instead of
void, and use the return
keyword inside the
function:
Example
#include <iostream>
using namespace std;
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
Storing the result in a
variable
#include <iostream>
using namespace std;
int myFunction(int x, int y) {
return x + y;
}
int main() {
int z = myFunction(5, 3);
cout << z;
return 0;
}
Pass By Reference
Pass a reference to the function can also be used. This can be useful when you
need to change the value of the arguments:
Example:
#include <iostream>
using namespace std;
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z; }
int main() {
int firstNum = 10;
int secondNum = 20;
cout << "Before swap: " << "\n";
cout << firstNum << secondNum << "\
n";
swapNums(firstNum, secondNum);
cout << "After swap: " << "\n";
cout << firstNum << secondNum << "\
Thank You
Coming up next:
Pointers in C+
+