// 17. Swap two integers using pointers, with two different functions.
//
// Note: C has no "reference variables" (int &x is C++ only). In C,
// "call by reference" means passing the ADDRESSES of the variables,
// which the function receives as pointers. This program shows:
//   swap_by_value()     - gets copies, so main's variables do NOT change
//   swap_by_reference() - gets addresses (pointers), so they DO change
#include <stdio.h>

void swap_by_value(int x, int y) {
    int temp = x;
    x = y;
    y = temp;
    printf("Inside swap_by_value:     x = %d, y = %d\n", x, y);
}

void swap_by_reference(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a, b;

    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    printf("\nBefore swapping:          a = %d, b = %d\n", a, b);

    swap_by_value(a, b);
    printf("After swap_by_value:      a = %d, b = %d  (unchanged)\n", a, b);

    swap_by_reference(&a, &b);
    printf("After swap_by_reference:  a = %d, b = %d  (swapped)\n", a, b);

    return 0;
}
