21 Jan C++ Functions
In this lesson, learn C++ Functions. A function is an organized block of reusable code, which avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a function, set the task in it, and call the function multiple times whenever you need it.
Create and call a Function
To create a function in C++, set the name of the function followed by parentheses:
1 2 3 4 5 |
void demoFunction() { // write the code } |
A function in C++ gets executed when it is called:
1 2 3 |
demoFunction() |
Let us now see an example to create a function in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; // Demo function void demoFunction() { cout<<"This is a demo function in C++.\n"; } int main() { // Calling the function demoFunction(); return 0; } |
Output
1 2 3 |
This is a demo function in C++. |
Function Declaration and Definition
In the above example, we saw how to create and call a function in C++. We defined and declared the function as well:
1 2 3 4 5 |
void demoFunction() { // function declaration // function body i.e., the definition } |
As shown above, a function has two parts:
- Function Declaration
- Function Definition
We can also write the above C++ function by separating function definition and declaration. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; // Function Declaration void demoFunction(); int main() { // Calling the function demoFunction(); return 0; } // Function Definition void demoFunction() { cout<<"This is a demo function.\n"; } |
Output
1 2 3 |
This is a demo function in C++. |
Function Parameters
Set the parameters in a C++ function after the name of the function. For example:
1 2 3 |
void demoFunction(int rank) { } |
To call a function with a parameter, set the value of the variable while calling, for example:
1 2 3 |
demoFunction(2) |
Therefore, we have passed the rank with the value 2 while calling above.
Let us now see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; // Function with a parameter void detailsFunc(int rank) { cout<<"Rank = "<<rank; } int main() { // Calling the function detailsFunc(2); return 0; } |
Output
1 2 3 |
Rank = 2 |
Multiple Parameters
We can also set more than one parameter in a function in C++. For example:
1 2 3 |
void demoFunction(string player, int rank, int points) { } |
To call a function with multiple parameters, set the value of the variable while calling, for example:
1 2 3 |
demoFunction("Amit", 1, 90) |
We have passed the following multiple parameters while calling above:
- player: Amit
- rank: 1
- points: 90
Let us now see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; // Function with multiple parameters void detailsFunc(string player, int rank, int points) { cout<<"\nPlayer = "<<player; cout<<"\nRank = "<<rank; cout<<"\nPoints = "<<points<<"\n"; } int main() { // Calling the function detailsFunc("Amit", 1, 90); detailsFunc("Jack", 2, 85); return 0; } |
Output
1 2 3 4 5 6 7 8 |
Player = Amit Rank = 1 Points = 90 Player = Jack Rank = 2 Points = 85 |
No Comments