C++ Functions

C++ functions are a group of statements in a single logical unit to perform some specific task.

Along with the main function, a program can have multiple functions that are designed for different operation.

The results of functions can be used throughout the program without concern about the process and the mechanism of the function.

c++ functions

C++ Functions

In POP (Procedural Oriented Programming) language like C, programs are divided into different functions but in OOP (Object Oriented Programming) approach program is divided into objects where functions are the components of the object.

Generally, C++ function has three parts:

  • Function Prototype
  • Function Definition
  • Function Call

C++ Function Prototype

While writing a program, we can’t use a function without specifying its type or without telling the compiler about it.

So before calling a function, we must either declare or define a function.

Thus, declaring a function before calling a function is called function declaration or prototype which tells the compiler that at some point of the program we will use the function of the name specified in the prototype.


return_type function_name(parameter_list);

Note: function prototype must end with a semicolon.

Here, return_type is the type of value that the function will return. It can be int, float or any user-defined data type.

function_name means any name that we give to the function. However, it must not resemble any standard keyword of C++.

Finally, parameter_list contains a total number of arguments that need to be passed to the function.

C++ Function Call

Function call means calling the function with a statement. When the program encounters the function call statement the specific function is invoked.


function_name( argument_list );

Here, function_name is the name of the called function and argument_list is the comma-separated list of expressions that constitute the arguments.

The syntax is similar to that of prototype except that return_type is not used.

C++ functions definition

Function definition is a part where we define the operation of a function. It consists of the declarator followed by the function body.


return_type function_name( parameter_list )
    function body;

Defining a function is a way of specifying how the operation is to be done when the function is called.

Illustration of function call

C++ Programming Functions

Whenever we declare a function a part of the memory is reserved for that function and when we define the function is stored in that memory block.

Let’s suppose the function is stored at the address 0x111. When we call the function in the main program compiler goes to that memory location 0x111 where the code is executed as defined.

After the execution, the compiler returns the result to the program without concerning any details how the result was obtained.

Though it takes time for execution, it becomes handy when dealing with huge programs.

General structure of a function in C++ program

//Structure of C++ program
#include <iostream>
using namespace std;
return_type function_name(parameter_list); //function prototype

void main()
  function_name();   //function call

return_type function_name(parameter_list)  //function defintion
  function definition