### Functions in C++

Hi, Welcome to the series of data structure and algorithm made easy, In this particular section we learn about function. a basic overview of the function and how function helps us to maintain neat and clean code function also improve the efficiency of the overall program.

**What is the Function and what is the importance of function?**

**10000 lines of code**and in this code we have around

**100 functions**and we call them function when we need it. and suddenly our code stop working and we are stuck but you remembred that you use the function in them and after running that code you get to know that

**error are in line 225,498,5222,6459**these are the line who make a program not to run and after some check, you know that

**only function 12 and 45 produce error**are called your 95% of work is already done you do not need to debug whole code you only need to debug function 12 and 45. and just after that your code is fine and you are good to go.

**What is the different phase in the function?**

**Function Declaration**:- in function declaration you declare the function the function with its return type, name, and list of perimeters.**Function Definition**:- in this phase, we define the function with its code what function is suppose to do and what function is return after the code executed.**Function Calling**:- function call is the last step of the function life cycle we call the function in main() function or in any other function we call a function to do the work we suppose the function does.

**What are the things that function contains?**

*return_type function_name(data_type parameter_1,data_type parameter_1........)*

*{*

*// function body*

*}*

**4 things**

- The return type of function means what data type a function return at the end of the function
- The function name is the arbitrary name provided by the user so that the user can remember at the time of function called.
- List of the perimeter is a list of all the perimeter that is passed to the function so that when a function does the work he can use the perimeter that needed.
- Last is Body of the function in the body of the function does the actual work that it should suppose to do and at the end, before the finish, it returns some value.

**with this example with and without function.**

*// Without the function*

**#include<iostream>**

**using namespace std;**

**int main()**

**{**

**int n;**

**cout<<"Enter number of element in array ";**

**cin>>n;**

**int arr[n];**

**for(int i=0;i<n;i++)**

**{**

**cin>>arr[i];**

**}**

**int sum = 0;**

**for(int i=0;i<n;i++)**

**{**

**sum = sum+arr[i];**

**}**

**cout<<"Sum of array is "<<sum;**

**}**

*// With the function*

**#include<iostream>**

**using namespace std;**

**int sumOfArray(int arr[],int n)**

**{**

**int sum = 0;**

**for(int i=0;i<n;i++)**

**{**

**sum = sum+arr[i];**

**}**

**return sum;**

**}**

**int main()**

**{**

**int n;**

**cout<<"Enter number of element in array ";**

**cin>>n;**

**int arr[n];**

**for(int i=0;i<n;i++)**

**{**

**cin>>arr[i];**

**}**

**int sumArr = sumOfArray(arr,n);**

**cout<<"Sum of array is "<<sumArr;**

**}**

So In this section we take only one example But in another section of

**Function Example**, we solve many other examples of a function.

Thank you so much for reading this article if you like this article then please do share this article with coder friends ðŸ˜€.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

## 0 Comments