C++ Tutorial C++ Advanced C++ Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ - Functions



A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. C++ has many built-in functions and one of the common function is main() which is used to execute code. Users can also create their own function which is also termed as user-defined functions.

Create Function

In C++, creating a function starts with defining return type of function followed by function's name and parenthesis containing function's parameter(s), if it has any. At last, it contains block of statements which is also called body of the function.

Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any and semicolon ;.

Syntax

//Defining function
return_type function_name(parameters) {
  statements;
}

//Calling function
function_name(parameters);

return_type: A function can return value(s). The return_type is the data type of the value the function returns. If a function does not return anything in that case void is used as return_type.

Example: A function with no parameter

In the example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute and has no return type, therefore void keyword is used.

#include <iostream>
using namespace std;
 
void HelloWorld(){
  cout<<"Hello World!"<<"\n"; 
}

int main (){
  HelloWorld();
  return 0;
}

The output of the above code will be:

Hello World!

Function Declaration

In C++, declaration of a function starts with return type of function followed by function's name and parenthesis containing function's parameter(s), if it has any. See the syntax below:

Syntax

//declaration of a  function
return_type function_name(parameters);

If the function is defined after it is called in the program, the C++ program will raise an exception. To avoid such situation, the function is declared before calling it, and the function can be defined anywhere in the program.

Example:

In the example below, the function called MyFunction is defined after it is called in the program. Since the function is defined after it is called in the program, program will raise an exception. To avoid such situation, function must be declared before calling it.

#include <iostream>
using namespace std;
//function declaration
void MyFunction(); 

int main (){
  //function calling
  MyFunction();
  return 0;
}
//function definition
void MyFunction(){
  cout<<"Hello World!."<<"\n"; 
}

The output of the above code will be:

Hello World!.

Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.

Example: A function with parameters

In the example below, the function called MyFunction is created which requires two integer numbers as parameters and print sum of two numbers in desired style. Please note that, the function returns nothing hence void is used as return type.

#include <iostream>
using namespace std;
void MyFunction(int x, int y); 

int main (){
  int a = 15, b = 10;
  MyFunction(a, b);
  return 0;
}

void MyFunction(int x, int y){
  cout<<"Sum of "<<x<<" and "<<y
      <<" is: "<<x+y<<"\n"; 
}

The output of the above code will be:

Sum of 15 and 10 is: 25

Function to Return Values

A function can be used to return values. To achieve this, user must have to define return type in definition and declaration of the function. In the example below, the return type is int.

#include <iostream>
using namespace std;
int MyFunction(int x, int y); 

int main (){
  int a = 15, b = 10;
  int sum = MyFunction(a, b);
  cout<<sum<<"\n"; 
  return 0;
}

int MyFunction(int x, int y){
  return x+y; 
}

The output of the above code will be:

25

Default Parameter Value

Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses default value.

Example:

In the example below, default parameter value is used to perform sum operation on two, three and four integer numbers using same function called MyFunction. Here, default value is set while declaring the function. The same can be done while defining the function also when function declaration is not used in the program.

#include <iostream>
using namespace std;
int MyFunction(int p, int q, int r=0, int s=0); 

int main (){
  int a = 15, b = 10, c = 5, d = 1;
  cout<<MyFunction(a,b)<<"\n"; 
  cout<<MyFunction(a,b,c)<<"\n";
  cout<<MyFunction(a,b,c,d)<<"\n";
  return 0;
}

int MyFunction(int p, int q, int r, int s){
  return p+q+r+s; 
}

The output of the above code will be:

25
30
31




5