C++ Programs:
Pass-By-Value:
# include <iostream.h>The output will be
int sub(int); //Function prototypemain()
{
int function_value;
int x = 99;
cout << "x before call: "<< x << endl;
function_value = sub(x); //Call function
cout << "x after call: "<< x << endl;
cout << "The value returned from function is "
<< function_value <<endl;
}// Definition of Function
int sub(int a)
{
a = a * 10;
return a;
}
x before call: 99Pass-By-Reference:
x after call: 99
The value returned from function is 990
# include <iostream.h>The output will be
int sub(int &); //Function prototypemain()
{
int function_value;
int x = 99;
cout << "x before call: "<< x << endl;
function_value = sub(x); //Call function
cout << "x after call: "<< x << endl;
cout << "The value returned from function is "
<< function_value <<endl;
}// Definition of Function
int sub(int &a)
{
a = a * 10;
return a;
}
x before call: 99Pass-By-Pointers:
x after call: 990
The value returned from function is 990
# include <iostream.h>The output will be
int sub(int *); //Function prototypemain()
{
int function_value;
int x = 99;
cout << "x before call: "<< x << endl;
function_value = sub(&x); //Call function
cout << "x after call: "<< x << endl;
cout << "The value returned from function is "
<< function_value <<endl;
}// Definition of Function
int sub(int * a)
{
*a = *a * 10;
return *a;
}
x before call: 99Note: Pass-By-Reference and Pass-By-Pointer get the same output.
x after call: 990
The value returned from function is 990