I have searched on Google, but I can't quite understand what is pass-by-value-result. What is the advantage and disadvantage of using pass-by-value-result?If I have a program code like this, what will be the result when parameters are passed by value-result?#include <stdio.h>int a=2;int b=1;void fun(int x, int y) {    b=x+y;    x=a+y;    y=b+x;}void main() {    fun(b,b);    printf(%d %d\n,a,b);}
What is pass-by-value-result and its advantage and disadvantage?
terminology;programming languages;semantics
Note: parameter passing by xxx is sometimes called pass by xxx andsometimes call by xxx.  I will use both expressions indifferently.Parameter passing by value-result consists in passing by value when thesubprogram (whatever it is) is called, and then, when it terminates,assign the current value of the formal parameter in the subprogramback to the actual parameter if it is a mutable entity, such as avariable or an array entry. Of course, no such assignment may occur ifthe actual parameter is a non mutable value providing entity such asan expression, a function call, a constant (though some old versionsof Fortran have been known to assign values to constants, so thatprinting 3 could actually print 5).Afaik, fortran was first to use this mechanism (I do not know forCobol). It is also available in Ada. Actually, Ada has call by value(IN parameter) and call by result (OUT parameter). OUT parameters arenot initialized at call, but assign current value of formal parameterto the actual parameter upon return. Parameter passing by value-result(IN OUT) is the combination of both. Actually, the case of Ada is morecomplicated (at least in its initial versions) because it wasexplicitly allowed to implement these parameter passing mechanism witha call by reference. I am not sure of the current status of thelanguage. This of course muddles the discussion below.Comparing parameters passing mechanisms is dependent on the languageconsidered.Issues you may want to consider are cost in time or space, readabilityand maintenance, compiling issues such as efficiency and optimization,and also semantics such as determinacy of programs, and also programmingconvenience. (I may have forgotten some).Many of these issue are stongly impacted by aliasingphenomena. Whenever the same mutable entity (possibly a simplevariable) can be accessed in different ways, it becomes harder toanalyse the program, determine what parts of the computation mayinterfere with other parts. When there are several threads ofcomputation, this may cause race conditions making the computationdependent on timing. When compiling program, aliasing may make it harderto get a reliable definition-use relation for performing someotpimizations.Hence,I would consider aliasing problems a major issue when comparingcomputing mechanisms.Since you need ways to communicate in programs, assessing a parameterpassing mechanism can only by comparing to alternatives.Regarding pass-by-value-result, the are only two that I see:Doing nothing on exit.Then you can pass information back to the caller only as functionresult, or by assigning a global variable visible from both calleeand caller. Using pass by reference.Then the formal parameter becomes justanother name, an alias, for the actual parameter, so that the actualparameter has the value of the formal one upon exit (as well asbefore exit).IssuesTime and space costOne advantage of call by reference over call by value-result is thatit is often cheaper, especially if the data to be passed is large(time/space costs of copying). Call by reference may have a time costbecause of indirect addressing, though that can sometimes be optimizedout.AliasingThe major drawback of call by reference is all I said aboutaliasing. So that may be considered a very good point forpass-by-value-result.Side effects and evaluation orderThen call by value-return has a problem when returning the values, asin your example. If two formal parameters (x and y in the example)actually get the same actual parameter (b in the example) then theactual parameter will take on return a value that depends on the orderchosen for returning values. Hence, it is essential to define it.  Inany case, such a situation is a sure indicator of bad programmingpractice.This problem does not exists with call by reference. But call byreference is a completely different kind of computation, as shown byexecuting your example in call by reference.Returning values is a side-effect on the actualparameters. Computations with side-effects always raise issues oforder dependence. If the evaluation of actuall parameters entails someside-effects (for example by calling a function with side-effects),then the order of evaluation of arguments may have an impact on thecomputation. That is why side-effect are to be avoided, or at leastcarefully managed.Objects identityIf you have a language that allows to distinguish identity andegality, the call by reference wil preserve identity, while call byvalue will not.  For example, when you copy an integer (for example byassignment), there is no way you can distinguish the new copy from theold one.  But Object-Oriented (OO) languages could let you make adifference, and have a test for equality (same value) and a test foridentity (same reference).Well definedness of call by value (irrespective of result)Another problem is that call by value may not be well defined. Youhave to determine what a value is. How deep do you copy an objectimplemented with references (pointers) when doing call by value.When a reference is not followed for copying, it is an open door foraliasing problems.I am not saying these issues are untractable, but only that you haveto deal with them consistently throughout the language (consistencywith assigment in particular). And that does not make for a clearcutanswer, independently of the rest of the language, and the style ofprogramming it supports.Running the example in call by value-result and call by referenceNotation     a<==b  a becomes aliased to b    a==b   a is alias of b  (reminder)    a<-3   a is assigned the value 3Call by value-result:     Call of fun(b,b): x<-1 y<-1 b<-2 x<-3 y<-5 return print 2 (3 or 5)   unpon returning, b ends up with 3 or 5 depending on the order of argument return.Call by reference:     Call of fun(b,b) x<==b y<==b b==b<-2 b==x<-4 b==y<-8 return print 2 8