# 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. There are some in-built functions in C++ 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. Please see the syntax below:

### Syntax

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


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 below example, a function called MyFunction is created to print Hello World!. The function requires no parameters to run and has no return type, hence void keyword is used.

#include <iostream>
using namespace std;

void MyFunction(){
cout<<"Hello World!."<<"\n";
}

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


Output

Hello World!.


## 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 (;). As in the above example, the function is called inside main() function using the following statement:

MyFunction();


## 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. Please 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 below example, 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";
}


Output

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 below example, 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";
}


Output

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 below example, 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;
}


Output

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 below example, 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;
}


Output

25
30
31


## Recursive function

A function which can call itself is known as recursive function. A recursive function generally ends with one or more boundary conditions.

### Example:

A function for calculating factorial using recursion method is described below.

#include <iostream>
using namespace std;
int factorial(int x);

int main (){
cout<<factorial(3)<<"\n";
cout<<factorial(5)<<"\n";
return 0;
}

int factorial(int x){
if(x==0)
{return 1;}
else
{return x*factorial(x-1);}
}


Output

6
120