C++ Function Call

There are two types of calling functions in C++: call by value and call by reference. These two are usually distinguishable from each other, with the types of values ​​passing them as parameters.
The parameter we passed to function is known as the actual parameter and the parameters obtained by the function are known as formal parameters.

Call By Value:

In the call by value method, the actual value of the parameter is copied into the function's formal parameter. If there is a change in the formal parameter value, this change will not reflect the actual parameter value.So there will be two types of parameters are stored in different memory locations.

Example :

void swapx(int x, int y); 
  
// Main function 
int main() 
{ 
    int a = 10, b = 20; 
  
    // Pass by Values 
    swapx(a, b); 
  
     cout<<"a="<< a<<"\t"<<"b="<< b<<"\n"; 
  
    return 0; 
} 
  
// Swap functions that swaps 
// two values 
void swapx(int x, int y) 
{ 
    int t; 
  
    t = x; 
    x = y; 
    y = t; 
  
     cout<<"x="<< x<<"\t"<<"y="<< y<<"\n"; 
}


Output :

x=20    y=10
a=10    b=20

Call By Reference

In a call by reference method, the actual value of the parameter is passed as a reference in the function's formal parameter. If there is a change in the formal parameter value, this change will reflect the actual parameter value. Then there will not be separate memory locations.

Example :

void swapx(int* , int* ); 
  
// Main function 
int main() 
{ 
    int a = 10, b = 20; 
  
    // Pass by Values 
    swapx(&a, &b); 
  
     cout<<"a="<< a<<"\t"<<"b="<< b<<"\n"; 
  
    return 0; 
} 
  
// Swap functions that swaps 
// two values 
void swapx(int* x, int* y) 
{ 
    int t; 
  
    t = *x; 
    *x = *y; 
    *y = t; 
  
     cout<<"x="<< *x<<"\t"<<"y="<< *y<<"\n"; 
}


Output :

x=20    y=10
a=20    b=10

Difference between Call by Value and Call by Reference


Call By Value Call by Reference
Calling function sends copy of data Calling function sends address of data
Formal parameter are ordinary variable Formal parameter are pointer variable
Atmost only one value can be sent back several result can be sent back
Actual parameter affected by change within the function Direct change are made to the Actual parameter
Values of variables are passes by Simple technique. Pointer variables are necessary to define to store the address values of variables.

Inline Function

The Inline function is a feature of c++, which is used to increase the execution time of the program. The inline function is the inbuilt function in C++. A function declared inside a class is automatically defined as inline. If a function is inline, the compiler keeps a copy of the code of that function at each point where the function is called at compile time.
If a change is made to the inline function, the function must be recompiled at each point because the compiler needs to change the code once again otherwise it will continue with the old functionality.

Advantages :

1. Reduces space because no separate set of instructions are written to memory.
2. It avoids the overhead of calling the actual function.
3. Using inline, you can put a function definition in a header file.

Example :

inline int Max(int x, int y) {
   return (x > y)? x : y;
}

// Main function for the program
int main() {
   cout << "Max (5,10): " << Max(5,10) << endl;
   cout << "Max (0,100): " << Max(0,100) << endl;
   cout << "Max (100,10): " << Max(100,10) << endl;
   
   return 0;
}


Output :

Max (5,10): 10
Max (0,100): 100
Max (100,10): 100

Exercise:-

1. Which from following is used for invoking a function?

A. call-by-reference
B. call-by-value
C. call-by-functions
D. Both A and B

View Answer


2. There are how many ways to invoke a function in C++?

A. 1
B. 2
C. 4
D. 8

View Answer



Program :-

Program to calculate squares and averages using the inline function.

#include 
using namespace std;

class Number
{
  private:
    int num;
  public:
    void getNumber(void);
    inline double square(void);
    inline double cube(void);
};

// defining these functions
void Number::getNumber (void)
{
  cout<<"Enter an integer number :";
  cin>>num;
}
inline double Number::square (void)
{
  return num*num;
}

inline double Number::cube(void)
{
  return num*num*num;
}

int main()
{
  Number objN;
  objN.getNumber();
  cout<<"\n SQUARE IS ="<< objN.square ();
  cout<<"\n CUBE IS ="<< objN.cube ();
  cout<< endl;
  return 0;
}
 


Output :

Enter an integer number :11
SQUARE IS =121
CUBE IS =1331





Visit :


Discussion


* You must be logged in to add comment.