Home >>C++ Tutorial >C++ Functions

C++ Functions

Functions in C++

In order to perform or execute any task, the functions are used in the C++ programming. Hence, it is also known as the procedure in various programming languages. The functions have many benefits like it can be called various times provide the reusability of the written code.

Advantages of Functions in C++

Here are the advantages that the programmer gets by the functions in C++

  • Reusability of the codeIn order to skip the writing of the code multiple times, the functions are created that can be called multiple times.
  • Optimization of the codeFunctions optimizes the written code so that the programmer don’t have to write much of the code.

Types of Functions in C++

There are generally two types of the functions in the C++ language:

  • Library FunctionsThese are the type of functions that are generally declared in the C++ header files like exp(x), cos(x) and more.
  • User-defined functionsThese are the type of the functions that are generally created by the user itself in order to use it multiple times. By using this function, the complexity of a large program is reduces and the code gets optimized.

Declaration of the Function in C++

Here is the syntax that is used to declare any function in the C++ programming:

return_type function_name(data_type parameter...)  
{    
//code that is to be executed    
}  

Here is an example of the simple C++ function that will help you understand the topic better:

#include <iostream>  
using namespace std;  
void demo() 
{    
   int i=10;    
   cout<<"i=" << i<<"\n";    
}    
int main()  
{  
 demo();    
 demo();
 demo();
} 
Output :
i=10
i=10
i=10

No Sidebar ads