[solved] why swap function is working well without prototyping? (C++) | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

[solved] why swap function is working well without prototyping? (C++)

Because I am using swap() function inside main() function so swap() function should be defined before the main() function OR it should be prototyped before main() function. But here in this program https://code.sololearn.com/cbD2NDDWru9J/?ref=app I did neither of them but still it is working without errors or warnings. On the other hand, if I try to do this with diff() function it gives error. Can anyone explain why this is not happening with swap() function?

14th Sep 2021, 6:30 AM
Yugal Kishore
3 ответов
+ 6
That is because you are not calling your own swap() function, but the one from the standard library, which is indirectly included via <iostream> in this case: https://en.cppreference.com/w/cpp/algorithm/swap You can verify this by trying to print anything inside your own swap() function. Also, since you are using pointers instead of references, the correct call to your own function would be this anyway: swap( &a, &b ); If you tried that without adding the prototype, you would then get an error as expected, because std::swap() is not overladed for addresses.
14th Sep 2021, 8:12 AM
Shadow
Shadow - avatar
+ 3
You're not calling your swap function but instead the swap defined in the standard library. https://en.cppreference.com/w/cpp/algorithm/swap This is the reason why using 'using namespace std' is a bad idea because if you didn't you'd have to explicitly call std::swap and you would have gotten an error otherwise in this case.
14th Sep 2021, 8:15 AM
Dennis
Dennis - avatar
0
Thanks.
14th Sep 2021, 8:37 AM
Yugal Kishore