+ 1
Can anyone explain🙏 about call by value and call by reference??
6 odpowiedzi
+ 1
Call by value: passes a copy of the value ,like: int a=5;
Then we pass a to a method that add it a+1 =6
If we return to our main main function now the place we declared a , and try to print it it will print 5 thar means whatever is changed on it it dose not update the orignal value
And the other one is the exact oppiset
+ 1
In C, if you want to call a function by passing parameters(variables) into it, you can do so by two methods: call by value & call by reference. Call by value means to pass the value of the variable whereas call by reference passes the variable itself into the function. If you need to change the original variable then use call by reference, unless you want to change the variable use call by value.
Call by value ->
int function(int x) { ... }
Call by reference ->
int function(int *x) { ... }
Notice the difference in the function definition of these two methods. Call by reference uses pointers, which tells the function that this points to the original variable x. After that, if you make any modification of the variable in the function the actual variable will be modified. But this doesn't happen in call by value, which only takes the value of the variable and returns something from that.
+ 1
https://www.javatpoint.com/call-by-value-and-call-by-reference-in-c
Here you'll find worked out example code in C. Try this out and it'll help you to understand the concept properly.
+ 1
#Abir Hasan
Thanks🥰
+ 1
Its nothing. Just if you pass by value. You are not changing the real value you are just sending the copy of the value . But if you pass by the reference you are passing the path to the real value which can affect the real value.
+ 1
It's a semantic difference but technically you always pass by value in C and C++. The pointers themselves are local copies pushed onto the call stack to hold the addresses you passed in. By accessing those pointers you can manipulate any data behind those addresses - but the pointers themselves are local to the function.
It's an important distinction, it means that from within the function you cannot change what the pointer points to and have it persist outside the function (without using a pointer to a pointer).
C++ lvalue references are just pointers underneath the hood but the language hides it from you. The only difference is that they're implicitly dereferenced (and therefore must not be NULL).