Ticker

20/recent/ticker-posts

Functions in C++ - Algomentor

Functions in C++ 


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?

A function is a particular block of code that maintains a code that does a particular task for us function help us to maintain the code and also help us to debug the code when we need it. Suppose we are writing a long code for software that around 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?

There are mainly three phases-in function
  1. Function Declaration:- in function declaration you declare the function the function with its return type, name, and list of perimeters.
  2. 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.
  3. 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
}

The function basically contain 4 things
  1. The return type of function means what data type a function return at the end of the function
  2. The function name is the arbitrary name provided by the user so that the user can remember at the time of function called.
  3. 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.
  4. 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.
Now we will do an example to demonstrate the function we are given an array we need to print the sum of the array we do 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;

}

//Here is the output(Both share same output)

Functions examples in c++

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.

Post a Comment

0 Comments