Programming in D – Solutions

Function Parameters

Because the parameters of this function are the kind that gets copied from the arguments, what gets swapped in the function are those copies.

To make the function swap the arguments, both of the parameters must be passed by reference:

void swap(ref int first, ref int second) {
    const int temp = first;
    first = second;
    second = temp;
}

With that change, now the variables in main() would be swapped:

2 1

Although not related to the original problem, also note that temp is specified as const as it is not changed in the function.