Send Close Add comments: (status displays here)
Got it!  This site "www.robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Procedure to swap values
by RS  admin@robinsnyder.com : 1024 x 640


1. Procedure to swap values
In sorting and other applications, it is useful to be able to swap the values of two variables, such as x1 and y2.

2. A first try
Will the following way work to swap the values of x1 and y2?


3. Program
Here is the C code [#2]


4. Output
What happened? Why were the values not properly swapped?

Here is the output of the C code.

The above did not work (unless the values of x1 and y2 are the same). Why not?

Note: It is possible to swap the values without an additional variable but not in just two assignment statements (using expressions without side-effects).

5. Another method
Here is an in-line method to swap the values using a temporary variable z3.


6. Program
Here is the C code [#4]


7. Output
This way worked.

Here is the output of the C code.

Let us abstract the above code into a swap procedure.

8. Procedure using value parameters
Here is the C code [#5]

Here is the output of the C code.

What happened? Why did swap not properly swap the values?

9. Call by value
In call by value, the value of the actual parameter is passed to the procedure/function. Since the procedure/function has only the value, the procedure/function cannot directly change the actual value of a variable passed as an actual parameter.

10. Value parameters
A value parameter is a formal parameter whose corresponding actual parameter value is passed to a procedure or function. If the actual parameter represents a memory location (e.g., a variable), assigning a value to the formal parameter within the procedure or function does not change the contents of the memory location represented by the actual parameter.

Since the swap procedure should change the actual values of the variable passed as an actual parameter, we cannot use call by value.

11. Call by reference
In call by reference, the memory address of the variable that is the actual parameter is passed to the procedure/function. Since the procedure/function has the memory address of the variable passed as the actual parameter, the procedure/function can change the value of the actual parameter. Explain the primary similarity and difference between call-by-value and call-by-reference.

12. Reference parameters
A reference parameter is a formal parameter whose actual parameter represents a memory location so that assigning a value to the formal parameter within the procedure or function changes the contents of the memory location represented by the actual parameter.

13. Reference parameters
Since the swap procedure should change the actual values of the variable passed as an actual parameter, we need to use call by reference.

Let us now try the above code with swap written using reference parameters.

14. Procedure using reference parameters
Here is the C code [#6]


15. Notation
Note that to use call by reference in C, the formal parameter name (and usage of the formal parameter) is prefixed with the asterisk "*" while the actual parameter name is prefixed with the ampersand "&".

16. Observation
In call by value, the actual parameter is an expression.

In call by reference, the actual parameter is a variable.

17. Guideline
How do you decide whether to use call by value or call by reference for a formal parameter?

Sometimes you are told which method to use.

Sometimes you have no choice (e.g., In c++, structures are always called using call by reference). In general, though, unless the actual parameter is a variable and the value of the variable passed as an actual parameter needs to be changed in the procedure/function, you should use call by value.

18. Preprocessor note
In production C/C++ systems, where machine efficiency is very important, a macro text-replacement strategy is often used for swapping values. Here is one way to do this using two variables and replacement and the C feature ignoring issues of overflow or underflow.

Note: This only works for integers. Other types require a more involved macro. Here is the C code [#7]

Note: Due to the replacement of swap(x1,y2) with a block (delimited by curly braces), a terminating semicolon can be used or not used. Here is the output of the C code.

The macro replacement by the preprocessor is a meta-programming process.

Note: You do not need this for this course, but you will often see such macros in production code, on the Internet, etc.

19. End of page

by RS  admin@robinsnyder.com : 1024 x 640