Types of Functions in C++

Types of Functions in C++

There are two types of functions in they are follows

    • Built-in functions
    • User-defined functions

1.Built-in Functions

Built in functions are functions that are already present in C++. The funtions are already placed in the header files and we can directly use this function in program. This functions is also known as library function

Example of built in functions

 <cmath>, <string>

2.User-defined Functions

The functions that a user can create their own function so it is called user defined function. We can defined the function in anywhere in Program and also can call them in any part of program.

Types of User-defined functions

    • No argument and no return value
    • No argument but the return value
    • Argument but no return value
    • Argument and return value

1. No argument and no return value

In this type of function, calling function passes no arguments to the function,and when it calls the calling function also the called function does not returns value to the calling function. Each function is independant

2. No argument but return value

In this type of function, it passes no arguments to the function, but there is a return value.

3. Argument but no return value

In this type of function, it passes the arguments to the calling function, but there is no return value.

4. Argument and return value

In this type of function, it passes the arguments to the calling function, and there is also a return value.

Function Call Methods in C++

The actual parameters pass values to a format parameters of function, this process is called  the passing of parameters.

There are two ways to call the function

      • Call by value
      • Call by function

1.Call By Value

In the pass by value technique of passing the parameter, you only pass the copies of the variable to formal parameter. So,the actual parameter and formal parameters are stored at different memory locations in program. In this calling method the original value does not modified.

Program of Call by value 

#include <iostream>
 using namespace std;
 void change(int num);
 int main()
{
 int num = 3;
 change(num);
 cout << "Value of the num is: " << num<< endl;
 return 0;
}
 void change(int num)
{
 data = 5; 
}

 

Output

Value of num is: 3

2. Call By Reference

Call by reference is the another technique to paasing the parameter to function. In this method, passing the adress or references to actual parameters.

Address or reference of the value is passed to the function, so actual argument and formal arguments share the same address space. So,  changed the value inside the function, is reflected inside and outside of the function

Program of call by reference

#include<iostream>  
using namespace std;    
void swap(int *x, int *y)  
{  
 int swap;  
 swap=*x;  
 *x=*y;  
 *y=swap;  
}  
int main()   
{    
 int x=200, y=300;    
 swap(&x, &y);
 cout<<"Value of x is: "<<x<<endl;  
 cout<<"Value of y is: "<<y<<endl;  
 return 0;  
}

Output

Value of x is: 300
Value of y is: 200

 

Read more, Function in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *