0

Why we use call by value and reference and what is meant by argument

22nd Apr 2017, 8:25 AM
Goudham Ram
Goudham Ram - avatar
5 ответов
+ 20
Although this code was written in Java, I think it'll help you to understand : https://code.sololearn.com/c613pBjuPn1q/?ref=app
22nd Apr 2017, 9:06 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
Difference between Call by Value and Call by Reference. In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. http://cs-fundamentals.com/tech-interview/c/difference-between-call-by-value-and-call-by-reference-in-c.php
22nd Apr 2017, 8:44 AM
Babak
Babak - avatar
+ 4
void func1(int a); // here func1 use the value of a but this value is just a copy, all the changes you do in the function about the variable a are modifications of a copy so it doesn't affect the real variable you've passed as a parameter. Also, be aware when you use this kind of function with objects and class, because it will call the copy constructor and if the class hasn't you will got an error. void func2(int &a); // here func2 get an access of the object a himself. It's a reference. So if you modify a in the function, the changes will be effective with the real variable you have passed to the function. Also this kind of function ia speeder than the first one because you don't copy the object you just give an access to this object. void func3(int const& a); // this is when you want to use the precedent function but when you want to be sure that a can't be modify. void func4(int* a); // this function takes as parameter a pointer and so you can modify the real object using the pointer
22nd Apr 2017, 9:04 AM
Glozi30
Glozi30 - avatar
+ 2
you call by reference whenever you want to modify the argument. also, it is faster. The reason is because when calling by value, the compiler makes a copy of the argument, whereas by ref, the object itself is passed.
22nd Apr 2017, 8:42 AM
Bebida Roja
Bebida Roja - avatar
+ 1
argument is the actual value that you pass to a parameter in function void yourFunction(int parameter){} yourFunction(12); //12 is the argument of parameter call by value, parameters references to a copy of a variable passed to the function to compute on it. Actual variable won't change: changes will apply on its copy. call by reference: parameter references the memory address of an object to compute on the object itself Any computation affects directly the object referenced
22nd Apr 2017, 8:36 AM
seamiki
seamiki - avatar