I want to know the difference between call by value and call by reference. Can you please give an example to explain it ?
Thank you for helping...
; )
Can anyone help me in C++ about call by reference?
By using "Call by Reference", we can modify values that passsed to a function. If we use "Call by Value", we can not modify value of parameters, because actually we just modify values that stored in stack, not in the memory.
Here is the example. In the main program, we pass a variabel contains number 1, and functions try to change it. Excuse me, my English is bad.
void test_one(int a) {
a = 2;
};
void test_two(int *a) {
*a = 2;
};
// C++
void test_three(int %26amp;a) {
a = 2;
};
void main(void) {
int a;
a = 1;
test_one(a);
printf("%d\n", a); // Result: 1;
a = 1;
test_two(%26amp;a);
printf("%d\n", a); // Result: 2;
a = 1;
test_three(a);
printf("%d\n", a); // Result: 2;
};
"%26amp;" sign means: Address of this variable in memory.
"*" sign means: Address that pointed by this memory address.
The best way to understand this, is by examine the Assembly Code of this program. You can use Turbo Debugger or just DEBUG.COM.
If you think my answer is the best answer, would you please do rate mine? Thank you.
Greetings from Tangerang, Indonesia.
Reply:Put very simply, when you make a function call involving parameters (the portion within the brackets), there can by two ways of doing this. In the call by value method, duplicates of the original are made in the function - any changes made to these values do not get reflected in the main function. On the other hand, when a call by reference is done, changes directly affect the parameters passed from the calling function. The latter is usually implemented by passing pointers to the variables.
One crude analogy can be thought of as the following - suppose that you were ill and had to ask your friend for his/her notes to copy because you missed classes. If the person were to copy his/her notes onto a separate piece of paper and hand it to you, that would be similar to calling by value - your notes are still unaffected, and you will have to sit and makes the notes all over again. On the other hand, if you were to pass your own notebook itself to the friend, and if s/he were sweet enough to copy the notes for you, that would be akin to call by reference.
Reply:C++ references are exactly like pointers (and thus you may apply anything you know about pointers) with two small differences:
1) they have to be initialized (you can't just declare a reference and leave it uninitialized)
2) you use the dot ('.') and not the arrow ('-%26gt;') to access its members
Reply:By using "Call by Reference", we can modify values that passsed to a function. If we use "Call by Value", we can not modify value of parameters, because actually we just modify values that stored in stack, not in the memory.
Here is the example. In the main program, we pass a variabel contains number 1, and functions try to change it. Excuse me, my English is bad.
void test_one(int a) {
a = 2;
};
void test_two(int *a) {
*a = 2;
};
// C++
void test_three(int %26amp;a) {
a = 2;
};
void main(void) {
int a;
a = 1;
test_one(a);
printf("%d\n", a); // Result: 1;
a = 1;
test_two(%26amp;a);
printf("%d\n", a); // Result: 2;
a = 1;
test_three(a);
printf("%d\n", a); // Result: 2;
};
"%26amp;" sign means: Address of this variable in memory.
"*" sign means: Address that pointed by this memory address.
Reply:Yea sure!
See , I bet u have heard passing variables from a function to the main.
U can call a function in 2 ways, by call by value method or by call by reference method.
When you are calling a function, u pass some parameters to the function that calculates the result out of these parameters according to yur wish.
The parameters you pass while function calling are called the actual parameters. During function definition the parameter list typed out is called the formal paramter.
IN call by value method, the actual paramters get copied to the formal parameters as a new variable. Any change to the formal paramters leads to no cjange in the actual parameters.
In call by reference method, actual paramter list is not copied but is used with a different name. It is the same variable with a different name. So obviously any change in the formal paramters has a direct effect upon the actual paramters.
Call by refernce method is useful for swapping variables.
Thats all u see!
(Sorry if I have accidentally swapped names with actual parameter and formal paramters!Sometimes I get confused!)
Reply:In c++, when functions are called, they can pass arguments either by value or by reference. The difference is that when the function passes arguments by value, it creates a new set of variables %26amp; copies the values of arguments into them. Hence any changes made during the function call are not made to the original variables! Whereas in Call by reference, the called function passes the address of the original variables, allowing the program to make changes in them during run-time.
Hope this helps!!!
Reply:in c++ there is a concept named as functions. in general programs are executed step by step, but if we want to do some task more than one time enywhere in program we use functions. example function for multiplication of two numbers. we can call this function any number of times with different arguments(values to be multiplied in this case).
eg. a = multiply(2,3);
b = multiply(5,78);
that was introduction to functions.
we can pass arguements to a function in two different ways:
1. call by value
2. call by reference
CALL BY VALUE:
when we pass arguements by value, it means the arguments we are passing to function are copied to that function in some new variables locally in that function, and changes made into these new variables doesnt reflect in our main function, and if we want changed value back in main function , we have to explicitly return that value..
CALL BY REFERENCE:
in c++, when variables are declared it gets some space in memory that also have some address or location number by which it is referenced. and we can store any value now in this location, but address remains same.
so in call by reference instead of passing value to a new function we passes the address of that variable so now if any channges made to value at that address that will also reflect in main function and we dont need to retuen this changed value as its already visible in main function.
example:
call_by_value_multiply(int a, int b, int c)
{
a = b*c;
cout%26lt;%26lt;"\nproduct = "%26lt;%26lt;a;
}
call_by_reference_multiply(int *x, int *y, int *z)
{
*x = (*y) * (*z);
cout%26lt;%26lt;"\nproduct = "%26lt;%26lt;(*x);
}
void main()
{
int a,b,c;
a=0,b=5,c=8;
call_by_value_multiply(a,b,c);
cout%26lt;%26lt;"\nproduct value in main = "%26lt;%26lt;a;
call_by_reference_multiply(%26amp;a, %26amp;b, %26amp;c);
cout%26lt;%26lt;"\nproduct value in main = "%26lt;%26lt;a;
}
this program's output will be
product = 40
product value in main = 0
product = 40
product value in main = 40
i think you got your answer now
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment