Functions are one of the most important building blocks in C++ programming. Functions allow developers to divide a large program into smaller and manageable parts. Functions also help in reusing the same code multiple times without writing it again.
In this chapter, you will learn what functions are in C++, why they are used, and how to create and use them in your programs.
In C++, a function is a block of code that performs a specific task. It can be reused multiple times within a program. Functions are also known as procedures or subroutines in other programming languages.
Functions make programs more organized because the same code can be reused in different parts of a program.
Functions help achieve modular programming. Modular programming means breaking a program into smaller, independent parts (modules) so they are easier to develop, understand, and maintain.
You can create a function by defining its return type, function name, and parameters, followed by a block of code that performs the task. Once created, the function can be called multiple times whenever needed.
Here is the syntax to create a function:
The syntax of a function is made up of the following main components:
In this example, we create a simple addition program using a C++ function to understand how functions work.
Output:
Sum: 12
In C++, functions can be defined and declared individually. The declaration is also known as a function prototype thaty informs the compiler about the function’s name, return type, and parameters without providing the function's body. Function initialization refers to defining a function and then calling it with actual values.
A function declaration informs the compiler about the function before it is used. It can be declared globally (outside all functions) or inside a class (as a member function). This ensures the compiler knows the function’s name, return type, and parameters before it is called.
Here is an example of function declaration:
A function definition contains the actual body of the function where the task is performed. Here is an example of the function definition where we are defining the task that will perform by the function.
Calling a function means using it in the program by passing actual values (arguments) to execute its code and get the desired result.
Consider the following example, where are defining and calling a function:
Functions can accept parameters in different ways depending on how values are passed.
In Pass by Value, a copy of the actual parameter is passed to the function. It means any modifications made inside the function do not affect the original value.
Let us take an example to illustrate the pass by value in C++.
Output:
5
In pass by reference, the actual variable is passed to the function using a reference. It means any changes made inside the function directly affect the original value.
Let us take an example to illustrate the pass by reference in C++.
Output:
25
In pass by pointer, the address of the variable is passed using pointers. The function modifies the value stored at the given address.
Let us take an example to illustrate the pass by pointer in C++.
Output:
Before: 10 After: 20
In C++, functions are mainly classified into two types:
Library functions are predefined functions provided by C++ and declared in header files. You can directly use them in your program by including the required header.
Some common examples:
This example demonstrates the use of library function.
Output:
Ceil value: 3 Cos value: -0.666276 Exp value: 9.97418
User-defined functions are created by programmers to perform specific tasks. These functions help reduce code repetition and make programs easier to understand and manage.
In this example, we use a function to understand how static and local variables behave.
Output:
i= 1 and j= 1 i= 2 and j= 1 i= 3 and j= 1
In this example, we create functions to check whether a number is an Armstrong number.
Output:
Enter a number: 153 153 is an Armstrong number.
Explanation:
In this example, we check whether the given number is Armstrong or not. The countDIgits() function counts the number of digits in the number by continuously dividing by 10 until the number becomes 0. After that, the isArmstrong() function extracts each digit, raises it to the power of digits and adds it to the sum. At the end, it checks the sum with the original number and returns a true or false value.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India