C++ Functions

Last Updated : 6 May 2026

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.

What are Functions in C++?

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.

Creating a Function in C++

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:

Explanation of Syntax

The syntax of a function is made up of the following main components:

  • Return Type: It specifies the value type, which is returned by the function. If the function does not return any of the values, the return type is void in the program.
  • Function Name: It is an identifier that defines the name of the function.
  • Parameter list: A comma-separated list of parameters is accepted by the function. Every function parameter has a type and a name.

Example of C++ Function

In this example, we create a simple addition program using a C++ function to understand how functions work.

Output:

Sum: 12

Definition and Declaration of a Function

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.

Function Declaration (Prototype)

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:

Function Definition

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.

Function Call (Initialization)

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:

Function Parameters

Functions can accept parameters in different ways depending on how values are passed.

1. Pass by value

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.

Example

Let us take an example to illustrate the pass by value in C++.

Output:

5

2. Pass by reference

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.

Example

Let us take an example to illustrate the pass by reference in C++.

Output:

25

3. Pass by Pointers

In pass by pointer, the address of the variable is passed using pointers. The function modifies the value stored at the given address.

Example

Let us take an example to illustrate the pass by pointer in C++.

Output:

Before: 10
After: 20

Types of Functions

In C++, functions are mainly classified into two types:

1. Library Functions

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:

  • ceil(x): It returns the smallest integer greater than or equal to x
  • cos(x): It computes the cosine of x
  • exp(x): The function exp(x) calculates the value of e when it is powered by x.

Example of Library Function

This example demonstrates the use of library function.

Output:

Ceil value: 3
Cos value: -0.666276
Exp value: 9.97418

2. User-defined functions

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.

Example 1: Basic User-Defined Function

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

Example 2: Armstrong Number using Function

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.