Can any one explain call by value and call by reference in simple manner? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

Can any one explain call by value and call by reference in simple manner?

Its difficult for me to understand. Thanks in advance.

14th Feb 2018, 6:19 AM
Ashish Kumar Jha
20 Answers
+ 69
This is an overly simplified illustration, but might just make sense, imagine you have a book, then you wanna lend that book to a friend. Call by value means you lend the book to your friend but s/he must make a copy of the book and use the copy, s/he may not write anything in your book, not even a single letter, it's just against the agreement. When you get your book back it is intact, unaltered. Call by reference means you lend the book and give your friend the right to do what they want with it, they may write on it, change what you have written, erase any writings you have done in the book, or even tear off some pages, they have full access to the book. When you get your book back it may have been changed, either in content, appearance, or usability. Your friend in this plot plays a role as a function or method. The book, is an argument you pass to the function. And you, role as the function caller. Hope this helps even just a tiny bit : ) Hth, cmiiw
14th Feb 2018, 7:19 AM
Ipang
+ 18
both purpose is the same to pass a value but when we mean pass by value is going to shop and buy what ever is in that shop rather in passing a value in reference is goes to the shop sales man told you where you can find the thing what you want
15th Feb 2018, 3:48 AM
Daniel Belay Akele
Daniel Belay Akele - avatar
+ 13
@Ipang, that is a good illustration of pass by reference Vs value. Also note just like with book passing by value takes longer as a copy has to be made. This doesn't make a difference with a data type like int, but with a large data structure can make a noticeable difference. Passing by reference implies that you trust the function with the data, ie it will only change what is supposed to. (Hopefully your friends don't ruin your books!)
14th Feb 2018, 9:15 AM
Jared Bird
Jared Bird - avatar
+ 13
In call by reference, whatever you do with the function's parameters, same will happen to the the calling function's parameters at the same time. But in call by value, the change is not reflected back. You need to know that you can also pass both kind of arguments in the same function.
16th Feb 2018, 12:50 PM
Sachin
Sachin - avatar
17th Feb 2018, 2:42 PM
Vukan
Vukan - avatar
+ 11
You can figure it this way: Call by value: you are passing a copy of the value as argument. The function cannot change the original argument. Call by reference: you pass a reference to the memory position that contains the value, as the argument. So the function can change what is contained in that memory position (the original argument).
16th Feb 2018, 5:16 AM
Half Byte
Half Byte - avatar
+ 10
@Jared Bird, yes making the copy involves memory allocation which may bring performance degradation depending on complexity of the "book". Thank you for adding that important measures, I skipped that part somehow. (I certainly hope they won't, books are a bit expensive for me : )
14th Feb 2018, 9:28 AM
Ipang
+ 9
There are two ways to pass value or data to function: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference. CALL BY VALUE: In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main(). Here Actual and formal arguments will be created in different memory location. CALL BY REFERENCE: In call by reference, original value is modified because we pass reference(address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.Here, Actual and formal arguments will be created in same memory location.
14th Feb 2018, 11:51 PM
Diwakar
Diwakar - avatar
+ 8
https://code.sololearn.com/c1Ttx1zeopsn/?ref=app I hope that this code can give you a better idea of call by value and call by reference mechanisms.
15th Feb 2018, 4:36 PM
Nashat
Nashat - avatar
+ 7
call by value:any change in the formal parameter does not reflect back to the actual parameter.here,the functions creates it's own copies of values and accesses it.any change made in these copies does not reflect back to the calling function. call by reference:any change in the formal parameter is reflected back to the actual parameter.here,the function assesses the original values using their references.therefore,any change made in these will reflect back to calling function.
17th Feb 2018, 9:40 AM
Nicky
Nicky - avatar
+ 3
call by Value will pass the value inside the Variable to a method,call by reference pass the Address of the Variable to the method,that means it pass the Address of that specific Variable in the Memory of the Computer and it acts just like a pointer,
18th Feb 2018, 4:27 PM
Joel Roy
Joel Roy - avatar
+ 3
lol..this question was in our cs exam today.....do u need a pic of q&a...?
8th Mar 2018, 1:13 PM
STRIKER
+ 2
call by value : value sent in call has no relation with original value ( value changed after call would not affect the original value). While in reference both original and value sent in call as parameter has relationship( one change another will also change)
17th Feb 2018, 2:27 PM
hamid khan
hamid khan - avatar
+ 2
in call by value method you only pass the value of the variable or value while calling the function.in call by reference method you pass the address(& variable will be used in function definition) if the variable.so if there are any changes happening to the variable which is passed will happen in its memory. hence when u use that variable called by reference after running the function the final value of the of variable after the function run will be stored in it.
17th Feb 2018, 6:01 PM
Dream
Dream - avatar
+ 2
Call by value is photocopy of a document wat ever changes u do in photocopy doesn't affect the original copy all changes r done in photocopy.. Call by reference is changes made in original documents,wat ever changes u do reflect to the original one. C++ coding....... void change(int a) {a=a*10;}//call by value changes made in 'a' reflecting inside the function body to 'a' void change(&a) {a=a*10;}//call by reference changes made in 'a' will reflect to inside and outside of function body
18th Feb 2018, 12:52 AM
Sunita Singh
Sunita Singh - avatar
18th Feb 2018, 2:27 PM
Patel Nisarg
Patel Nisarg - avatar
+ 1
for example when we want to use the swap function we must use call by reference to exchange or to swapen the values.
18th Feb 2018, 6:04 PM
Karim
+ 1
first EVERY variable has memory location. Memory has 2 things address and value. Call by value As the name suggests only value of the variable is copied or used in the called function.the value of variables called remain unchanged Call by reference In this the address and value of the variable is called .i.e the effect of any operation on the variable inside the called function will affect the value of actual variable.
19th Feb 2018, 2:11 AM
Sufal Chhabra
Sufal Chhabra - avatar
+ 1
A simple way of explaining it without an analogy and with a little more behind the scenes detail: Call by Value: When the programmer calls a function and passes some variable as an argument (I.e. Func(x)) the value of the variable x will be copied to the formal parameter of the function (I.e. Func(arg1)) the variable arg1 inside this function lives in a different memory location than the argument variable passed in (x) this means that anything that happens to arg1 inside Func() will NOT have any affect on the variable x because it lives in a different location in memory (I.e. it's a copy of x) Call by Reference: This is simply the opposite, going off of my previous example: The formal parameter of the function (arg1) will not live in a different location in memory it will in fact be a reference to the same location in memory as x (I.e. Arg1 is not a copy of x it is actually x). Therefore anything done to arg1 inside of Func() will affect the variable x Hope this helps and gives a decent insight on how the two methods of passing arguments actually works
19th Feb 2018, 4:18 AM
Dpats13
+ 1
in call by value,any change in the formal parameter are not reflected back to actual parameter in call by reference any change in the formal parameter are reflected back to actual parameter call by value eg: int a=5; { cout<<"a"; change(a); cout<<"a"; return0; } void change(int b) { b=10; } output: a5 b5 in call be ref output will be a5 b10.
10th Mar 2018, 6:35 AM
Durg@😊