Why is it that if I have a function like this, to swap two numbers, it doesn't work[swap], (I know I can do this by declaring pointers in the prototype, and then pass the address of the respective variables in main()), but works for array, without having to pass pointers and addresses.
Doesn't work
void num_exchange(int m, int n); int main(){ int num1 = 5; int num2 = 6; num_exchange(num1 , num2 ); cout << "num1 =" << num1 << endl; cout << "num2 =" << num2 << endl; return 0; } void num_exchange(int m, int n){ int temp; temp = m; m = n; n = temp; } Works
void arr_exchange(int [], int); int main(){ int n[7] = { 0, 0, 0, 0, 0, 0, 0 }; arr_exchange(n, 7); for (int i = 0; i < 7; i++) cout << n[i] << " "; return 0; } void arr_exchange(int x[], int){ for (int i = 0; i < 7; i++) x[i] = 1; } 4 Answers
void num_exchange(int m, int n){ int temp; temp = m; m = n; n = temp; } modifies copies of the input integers. To make your code work use
void num_exchange(int& m, int& n){ int temp; temp = m; m = n; n = temp; } instead (note the & in the first line). This is called passing by reference. In general, use std::swap to swap things.
void arr_exchange(int x[], int){ for (int i = 0; i < 7; i++) x[i] = 1; } works because in C++
void arr_exchange(int x[], int){ is equivalent to
void arr_exchange(int* x, int){ So here a pointer is passed and thus the original data is modified.
2Your num_exchange() takes its arguments by value, i.e., it sees copies of the original value. When you swap them, the copies are swapped but the originals are not touched. When you swap arrays, instead a pointer to the array start is passed (built-in arrays are never copied directly in C++). Since the pointer references the original array rather than a local copy, swapping array elements results in a change visible even when the function is exited.
To pass the actual original objects to your num_exchange() function you'd either use pointers, too, or you'd use references, e.g.:
void num_exchange(int& m, int& n) { // ... } Of course, the easiest approach to swapping elements is to use the standard or type provided swap() function although it is, admittedly, a bit awkward to use due to the extra using-directive:
using std::swap; swap(num1, num2); Functions are by default call by value.
what happens when a function call occurs???
When a function call occurs address of next instruction in program is pushed onto stack which tells the compiler where to return after function execution following that the function arguments(5 and 6) are pushed onto stack. Inside function, these arguments are popped out of stack i.e. m=5,n=6 this means the actual arguments(num1,num2) are unaltered. The function then executed with aliases of num1(i.e. m) and num2(i.e. n) after execution the address is popped out of stack to continue execution from where it is left.
In C, solution to this problem is to pass address of num1, num2 and use pointers to inside function to point them.
CPP added new concept called reference.A reference is another name given to the variable.
int & n1 = num1; int & n2 = num2; why it works with arrays??
Copying whole array is not feasible hence arrays are always pass by reference.The base address of array is passed to function hence whatever modifications done to the array in the function affects the real array.
the code you are loking for need two pointers because the function will copy the data you give them so:
void swap (int * a, int * b){ int temp = *a; *a = *b; *b = temp; }