Wednesday 11 April 2018

Passing Parameters Types in C/C++


Parameter passing is also divided to the following types:
1.      Pass by value.
2.      Pass by reference.
            Pass by value
In pass by value method the value of actual parameter is passed to the formal parameter in the function header at the time of function call.

The previous example uses “pass by value” method.

Pass by reference
In pass by reference method the address of the actual parameter is passed to the formal parameter in the function header at the time.
In this method the values of actual parameters can be changed by the Formal parameters.

Example:
The above example in “pass by value” is now “pass by reference”:

#include<conio.h>
#include<iostream.h>

void square_of_value(int&); à function declaration

void main()
{
clrsrc();
int value = 0;
cout<< “ Enter a value: “ ;
cin>> value;                                                                  
square_of_value( value );  à function call
getch();                                                                               
}

void square_of_value(int &num) à function header
{
cout << “square of the given number is: ”<< num ;
}

No comments:

Post a Comment