In: Computer Science
Give one example that will show that pass by value, pass by Reference, pass by name, pass by value result are all different.
Examples:
pass by value:
void incrementCount(int count)//pass by value
{
count=count+1;//increments the value of count inside the function
}
int main()
{
int count=0;// initialze the variable count
int result=0;// initialze the variable result
incrementCount(count);//call increment function
cout<<"Pass by value\n";
cout<<"Count:";
cout<<count;//prints the value of count after the function call
return 0;
}
pass by reference
void incrementCount(int & count)//& to pass by reference
{
count=count+1;//increments the value of count
}
int main()
{
int count=0;//initialize the variable count
int result=0;// initialize the variable result
incrementCount(count);//increment value of count
cout<<"Pass by Reference\n";
cout<<"Count:";
cout<<count;//prints count after the function call
return 0;
}
pass by name:
real procedure Sum(j, lo, hi, Ej);
  value lo, hi;
  integer j, lo, hi; real Ej;
begin
  real S;
  S := 0;
  for j := lo step 1 until hi do
    S := S + Ej;
  Sum := S
end;
pass by value result:
#include <iostream>
#include <string.h>
using namespace std;
void swap(int a, int b)
{
  int temp;
    temp = a;
    a = b;
    b = temp;
}
int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};
  swap(value, list[0]);
  cout << value << "   " << list[0] << endl;
  swap(list[0], list[1]);
  cout << list[0] << "   " << list[1] << endl;
  swap(value, list[value]);
  cout << value << "   " << list[value] << endl;
}