C++ functions Brief Explanation

Ram Pothuraju



A function is block of code which is used to perform a particular task, for example let’s say you are writing a large C++ program and in that program you want to do a particular task several number of times, like displaying value from 1 to 10, in order to do that you have to write few lines of code and you need to repeat these lines every time you display values. Another way of doing this is that you write these lines inside a function and call that function every time you want to display values. This would make you code simple, readable and reusable.


Syntax of Function



return_type function_name (parameter_list)
{
   //C++ Statements
}


A simple function example
#include <iostream>
using namespace std;
/* This function adds two integer values
 * and returns the result
 */int Addition(int n1, int n2){
   int n3 = n1+n2; return n3;
}

int main(){
   //Calling the function
   cout<<Addition(1,99);
   return 0;
}

Output:

100


The same program can be written like this




#include <iostream>
using namespace std;
//Function declaration
int Addition(int,int);

//Main function
int main(){
   //Calling the function
   cout<<sum(1,99);
   return 0;
}
/* Function is defined after the main method 
 */
int Addition(int n1, int n2){
   int num3 = n1+n2;
   return n3;
}




Function Declaration: 

You have seen that I have written the same program in two ways, in the first program I didn’t have any function declaration and in the second program I have function declaration at the beginning of the program. The thing is that when you define the function before the main() function in your program then you don’t need to do function declaration but if you are writing your function after the main() function like we did in the second program then you need to declare the function first, else you will get compilation error.

syntax of function declaration:


return_type function_name(parameter_list);

Note: While providing parameter_list you can avoid the parameter names, just like I did in the above example. I have given int Adiition(int,int); instead of int Addition(int n1,int n2);.

Function definition: Writing the full body of function is known as defining a function.
syntax of function definition:

return_type function_name(parameter_list) {
    //Statements inside function
}

Calling function: We can call the function like this:

function_name(parameters);
Now that we understood the working of function, lets see the types of function in C++

Types of function
We have two types of function in C++:

1) Built-in functions
2) User-defined functions

1) Build-in functions
Built-in functions are also known as library functions. We need not to declare and define these functions as they are already written in the C++ libraries such as iostream, cmath etc. We can directly call them when we need.

2) User-defined functions
The functions Which Is Defined By The User Is Called User Defined Functions


Post a Comment

0Comments

Post a Comment (0)