20130411

How to distinguish between call by value, call by reference, call by address

/*How to distinguish between call by value, call by reference, call by address.
Program to distinguish between pass by value,pass by reference,pass by address. Call by reference and call by address affects the calling value while call by value doe not. Most important difference is that in call by value new object is created which is some times such as when size of the variable is too large is not desirable so call by reference is preferred over call by value*/

#include<stdio.h>
#include<iostream>
using namespace std;
void callbyreference(int * x);//pass by address
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(&x);
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyreference(int * x)
{
*x=*x+5;//mind the*
}


//CALL BY REFERNCE, PASS BY REFERNCE
/*

void callbyrefernce(int & x);
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyrefernce(x);//pass by refernce
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyvalue(int & x)
{
x=x+5;
}
*/

//CALL BY VALUE, PASS BY VALUE
/*
void callbyvalue(int  x);
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(x);//pass by value
printf("\nAfter x=%d",x);//10
printf("\n");
return 0;
}

void callbyvalue(int  x)
{
x=x+5;
}
*/
Previous                             Home                               Next

No comments:

Post a Comment