Write a program to swap the values of two number if values of both variables are not the same using user define functions

Pass-by-Value

In pass-by-value, a copy of the actual arguments is passed to the function. Any changes made to the parameters inside the function have no effect on the actual arguments.

Pass-by-Reference

In pass-by-reference, the actual arguments are passed to the function, allowing the function to modify the values of the arguments directly.

Program Implementation

Explanation

  1. swapByValue Function:
    • This function takes two integers as parameters by value.
    • It checks if the values are different, and if so, it swaps them using a temporary variable.
    • Since the function uses pass-by-value, changes made inside the function do not affect the original variables.
  2. swapByReference Function:
    • This function takes two integers as parameters by reference.
    • It checks if the values are different, and if so, it swaps them using a temporary variable.
    • Since the function uses pass-by-reference, changes made inside the function directly affect the original variables.

Output

Key Points

  • Pass-by-Value: The original values of x and y remain unchanged after the function call.
  • Pass-by-Reference: The original values of x and y are swapped after the function call