+ 2
How this code gets executed? Even I'm not returning any value from the function
2 Réponses
+ 6
Because you call this function, and void function didn't return value.
+ 5
Passing the actual arguments to formal parameters by reference/address is an usual and efficient way to pass the large data types like string or STL containers. In case of your example, that doesn't make a difference on overall performance, however, it makes it possible to keep track of changes which is going to be made inside the swap function. Also, passing two variables by value for swapping obliges the swap function to return two values as the result which makes the job a little complicated. As a side node, the swap function needs to check the equality of two numbers before swapping as
void swap(int *a, int *b) {
if (*a == *b)
return;
//...
}
____
Complementary read: https://www.learncpp.com/cpp-tutorial/74-passing-arguments-by-address/