Can anyone explain🙏 about call by value and call by reference?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone explain🙏 about call by value and call by reference??

14th Mar 2021, 3:21 PM
LazyCat😼
LazyCat😼 - avatar
6 Answers
+ 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
14th Mar 2021, 3:27 PM
Bshar
Bshar - avatar
+ 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.
14th Mar 2021, 3:44 PM
Abir Hasan
Abir Hasan - avatar
+ 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.
14th Mar 2021, 3:50 PM
Abir Hasan
Abir Hasan - avatar
+ 1
#Abir Hasan Thanks🥰
14th Mar 2021, 3:54 PM
LazyCat😼
LazyCat😼 - avatar
+ 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.
15th Mar 2021, 5:09 AM
Illa Bahadur Darlami Magar
Illa Bahadur Darlami Magar - avatar
+ 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).
15th Mar 2021, 1:21 PM
Mike A
Mike A - avatar