+ 2

How this code gets executed? Even I'm not returning any value from the function

https://code.sololearn.com/c95zNmbzK8XJ/?ref=app

2nd Oct 2018, 4:31 PM
M A Mohmad Ashik
M A Mohmad Ashik - avatar
2 Réponses
+ 6
Because you call this function, and void function didn't return value.
2nd Oct 2018, 8:27 PM
Izaak GOLDSTEIN
Izaak GOLDSTEIN - avatar
+ 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/
2nd Oct 2018, 9:04 PM
Babak
Babak - avatar