Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12
pass by reference is faster, because values are not copy unlike pass by value
20th May 2019, 9:41 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 10
A reference is a pointer in disguise. A normal pointer has eight bytes. So when it's a small value like an int, cbv should be faster. This is from my noob perspective - I should read up on these links next. ;-)
20th May 2019, 9:48 AM
HonFu
HonFu - avatar
+ 7
Obviously pass by reference! Since it uses only the address not the actual value . It uses less memory and eventually becomes faster👍🏻
20th May 2019, 2:46 PM
Magesh VS
Magesh VS - avatar
+ 7
No copying of values is required in pass by reference (other than copying of an address). Hence for larger data structures, pass by reference is faster. If you want to change the value of a variable within a function, pass by reference or pointer (thanks HonFu) are the only options.
20th May 2019, 10:25 PM
Sonic
Sonic - avatar
+ 4
Pass by pointer is another.
20th May 2019, 10:36 PM
HonFu
HonFu - avatar
+ 4
By reference it is faster for several reasons, essentially, because the copy of fact requires also finding the reference of a value to copy it, besides the copy process is linear in dependence of the length bytes of the data type that is being copied, while that the reference has a fixed length. But sometimes a copy of value is required so as not to affect the original variable and respect its scope. I do not think any one is better than another. But reference if it is faster.
20th May 2019, 11:43 PM
Oliver Valiente Oliva
Oliver Valiente Oliva - avatar
+ 4
If your data is a basic data type (int, float, etc.) both will be equally fast. Pass by value is a tad faster as you don't need a level of indirection when you access the passed data. If your data is a big fat struct, pass by reference will be faster as you don't have to copy the whole struct before passing it, you just pass a pointer to it. so it is better to pass scalars by value and classes/structs by reference. For more info you can refer this link. https://www.quora.com/Which-is-faster-in-runtime-and-efficient-memory-pass-by-value-or-pass-by-reference
21st May 2019, 1:24 PM
Shashank Sharma
Shashank Sharma - avatar
+ 3
Call by reference passes the pointer to object. Not the object itself. This is much faster than copping the obect. if your object is in array passing by reference send the address of the first element of the array. If you pass by value you would copy all array elements to the stack. my one warning is when you pass by reference if you change the value of a member of the object it will change it to the rest of the world ( calling program ).
21st May 2019, 6:25 PM
Rick Shiffman
Rick Shiffman - avatar
+ 2
pass by reference is faster then pass by value as it is easy to deal with address than with the data.
20th May 2019, 7:25 PM
Gara Ranjeet
Gara Ranjeet - avatar
+ 2
Pass by reference is faster than pass by value because in pass by reference you just pass a Pointer to it, execution of Pointer is more faster(direct memory access).
21st May 2019, 4:48 AM
Arun
Arun - avatar
+ 2
A reference is a pointer in disguise. A normal pointer has eight bytes. So when it's a small value like an int, cbv should be faster. ........ This will help you in C++.. 😊😊
21st May 2019, 7:11 AM
Ajay Singh Chandel
Ajay Singh Chandel - avatar
+ 2
Pass by reference is more efficient than pass by value, because of less storage and only pass reference that make it faster,
21st May 2019, 10:25 AM
Janbaz Khan
Janbaz Khan - avatar
+ 2
pass by reference is faster then pass by value because pass by reference cannot copy the actual parameters into formal parameters which helps to decrease execution time
21st May 2019, 3:44 PM
Ameer Wajid Ali
Ameer Wajid Ali - avatar
+ 2
Pass by reference or pointer is usually faster and more efficient as it deals with address. Pass by value cannot be used if you need to modify the original variable.
21st May 2019, 7:41 PM
zwu01
zwu01 - avatar
+ 2
first , if u pass ur parameters by value, whatever happens to the parameters inside the function, it stays inside the function and the value of that particular Parameter won't change in the main, second u can. have just one return value from a function by default hence in case u need more than one return parameter, u can define that parameter in the main and pass it by reference to the function then what ever u assign to it will be returned to main .... thus I don't say it's just about being more effective or faster ,, u have to consider the scopes ... but using reference is a faster option but u are actually using the copy of the parameter and It's value will be changed to whatever it will be inside the function
22nd May 2019, 5:11 AM
S/\/\:)LE
S/\/\:)LE - avatar
+ 2
As Rstar has pointed out, there are already similar inquiries: https://www.sololearn.com/discuss/1072418/ https://www.sololearn.com/discuss/1525982/ https://www.sololearn.com/discuss/221292/ Please search prior to posting. (keywords: "call" "value") Happy coding!
10th Jun 2019, 7:08 PM
Janning⭐
Janning⭐ - avatar
+ 1
Depends on the scenario 😉 - objects can only be passed by reference unlike values ...
21st May 2019, 12:33 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
In C++, there are three types to pass values are that 1. Call by value :- It will create copies of that variable and pass in function. So, original values are not passed. 2. Call by reference :- When we call by reference, by using & then alias of variable is created that is point to that original values. Then changes in alias are affected in original values. 3. Call by address :- In which, address of that variable is pass as an argument using pointer *.
21st May 2019, 12:39 PM
Malhar Naiya
Malhar Naiya - avatar