What is call by value

Posted by Ramanagudlur under C# on 4/18/2012 | Points: 10 | Views : 13597 | Status : [Member] | Replies : 7
Hi every body could u please explain with one exaple what is call by value and what is call by reference where we can use those please answer me my question

Thanks and Regards ,
Ramana,

ramanagv


Responses

Posted by: Santhiram on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Pass by Value :Means any changes that take place inside the calling method have no affect on the original value of the variable.Look the example below.Here in our example original value of n1 is 10 .When it is passed to "Test" method n1 is incremented.But it is not reflected in the main method and hence the final value of n1 remains same ie 10.

Pass by Reference: Any changes that take place inside the calling method will affect the original value of the variable."ref" keyword must be used in the calling method & called method.Here in our example original value of n2 is 10 .When it is passed to "Test" method n2 is incremented which is then reflected in the main method too and hence the final output value of n2 is 11.

Example:
class Program
{
static void Main(string[] args)
{
int n1, n2;
n1 = 10;
n2 = 10;
Test(n1, ref n2);
Console.WriteLine(n1 + " " + n2);
}

public static void Test(int a,ref int b)
{
a++;
b++;

}
}

Result : 10, 11

Thanks
Shanti


Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
By Val - will not change the actual value.

By Ref - will change the original value.

____________
www.flickr.com/photos/psdesigner/

Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hope this helps you

http://www.exforsys.com/tutorials/c-language/call-by-value-and-call-by-reference.html

____________
www.flickr.com/photos/psdesigner/

Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ramanagudlur on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hi all thak u very much for ur cooperation and concern


Thanks,
Ramana

ramanagv

Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Call By Value:
Creates a new memory loaction for use within the
subroutine.The memory is freed once it leaves the
subroutine.Changes made to the variable are not affected
outside the subroutine.
Call By Reference:
Passes a pointer to the memory location.Changes made to the
variable within the subroutine affects the variable outside
the subroutine.

------------------------------------------------
Learn throughout life

Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/19/2012 [Member] Bronze | Points: 25

Up
0
Down
There are two ways of passing arguments/parameters to a function. One method is Call by Value and the other method is Call by Reference.
Call by Value: in the Call by Value the programming is sending the copy of an argument/parameter to a function. Programmer is not passing the actual variable. Since only the copy of the variable is passed to the function therefore any changes/modifications made within a function don't affect the variable and the variable value remains the same after the function call.
The following example will clarify it;
Suppose a = 2 before the function call
Void function (int a)
{
a = a + a;
a = a - 5;
}
Since only a copy of variable "a" is passed to the function that's why even after the function call value of a will remain 2.
Call by Reference: in the call by Reference the address of the argument/parameter is passed to the function. In simple words the actual variable or a pointer is passed to the function and variable value can be changed/modified within a function
For example; suppose a = 2 before function call
After passing this value to a function like
Void function(int &a)
{
*a = *a + *a;
*a = *a - 5;
}

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 7/16/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Call by value:
void swap(int x, int y)

{
int temp;
temp = x;
x = y;
y = temp;
printf("Swapped values are a = %d and b = %d", x, y);
}

void main()
{
int a = 7, b = 4;
swap(a, b);
printf("Original values are a = %d and b = %d", a, b);
printf("The values after swap are a = %d and b = %d", a, b);
}

Output:
Original Values are a = 7 and b = 4
Swapped values are a = 4 and b = 7


Call by reference:

void swap(int *x, int *y)

{
int temp;
temp = *x;
*x = *y;
*y = temp;
printf("Swapped values are a = %d and b = %d", *x, *y);
}
void main()
{
int a = 7, b = 4;
swap(&a, &b);
printf("Original values are a = %d and b = %d",a,b);
printf("The values after swap are a = %d and b = %d",a,b);
}

Output:
Original Values are a = 7 and b = 4
Swapped values are a = 4 and b = 7


Ramanagudlur, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response