Could you please give me a little help on the following problem
Suppose we have the following pseudocode:
var i, j : integer;
procedure P (k,m:integer);
What are the values of i and j at the end of the program fragment if (a) both parameters are passed by value? (b) both parameters are passed by reference. ??
Adam, Thank you for your question. This appears not to be a C++ language question, but I will try to answer anyway.
Arguments "passed by value" are simply treated as values, not as memory locations. They are usually passed to a function via cpu registers. Arguments "passed by reference" are passed to a function as addresses of memory locations.
The main difference in practice is that
1. Changing a "by value" argument does not change the variable that was passed as the actual argument. In fact, some compilers prohibit this.
2. Changing a "by reference" argument changes the variable that was passed as the actual argument.
Note that an arbitrarily complicated expression (of the correct datatype) may be passed "by value" but not "by reference", since it is not a memory location. Some compilers (such as for Basic or Fortran) will evaluate such an expression and store it in a memory location, then pass a reference to that location. This is not pure "pass by reference".