C++ Functions6 min read

A function refers to a block of code (or a group of statements) that performs a specific task. A function takes some inputs, does some computation based on that input and returns the output. The function can be called many times by passing different values each time.

For example, let us say that you need to compute a complex multiplication for different values on a different part of a program. One thing you can do is repeat the block of code for computation again and again, which is not a good practice as a programmer and makes the program complex and time-consuming. Better you can create a function and call that function every time you need by passing the different values.

Every C++ program has at least one function which is always executed and that is the main function from where the execution of the program begins. The function is declared within the curly braces {}.

Advantages of functions in C++

There are many advantages of using function in C++.

Reusability of code: As mention above in the example, we can reuse the function by calling it again and again as required in the program.

Optimization of Code: Use of functions makes the program short and simple to understand. You only need to write the logic of the calculation once and use it several times.

C++ function is defined into two categories:

1. Standard Library Functions
2. User-defined functions

1. C++ Standard Library Functions

These are the pre-defined function in a C++ program, like a main function. These built-in functions are already defined and written in C++, to use them you just need to invoke or call them directly.

Some common library functions in C++ are sqrt()abs()isdigit(), etc.

To use these library functions an appropriate header file needs to be included in the program. Example: to use math functions such as sqrt(), you need to include cmath header file.

Example of C++ Standard Library Functions:

2. C++ User-defined functions

These are the functions created by the programmer in order to use it as many times as required. The user or programmer writes a block of code inside a body of a function to do some specific task.

These functions can also be called or invoke from any part of the program and code inside the body of that function will be executed.

We will see the examples of both Library Functions and User-defined functions below in this article.

Example of C++ User-defined Functions:

Function Declaration or Prototype:

Declaring a function means telling the compiler about the function name and its return type and parameters (if any). it is like stating the prototype of the function.

In C++, the function declaration declares a function without a body. It is important to declare the function when you are using one of the source file function to another file. We declare the function at the top of the program.

The syntax for function declaration:


Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required

Function Definition

Generally, the function is defined in following way in C++:

Each part of a function:

  • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
  • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
  • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
  • Function Body: The function body is enclosed within the curly braces’{}’. It defines the task of a method within the curly braces.

Note: The function declaration and definition is for the user-defined type function only because the library function is already declared and defined. We just need to call the library function in order to use it.

Function Call

We need to call the function in order to use it. To call a function we simply write the function name by passing the values as parameters (if any).

When the function is called, the execution control passes to the function and does some specific task. When the return statement of the function is executed or when it executes the end braces, it means the program control is returned to the main function.

Following is the syntax t call a function.

Example: How to declare and define a function and call the function in C++


Function Arguments

The function is called by passing some values, and the function must declare variables in order to accept those values of the argument. These variables are called a formal parameters of the function and created during the entry of the function.

The actual arguments refers to the values passed while calling the function.

There are two ways in which the argument can be passed through a function:

  • Call by Value.
  • Call by Reference.

Click here to learn about them in detail.


Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More