0

I have just started learning c++ and i came across this concept of function declaration. It says "a function must be declared before use" but i have written a program where i have commented the function declaration and used the function. My program is still working. I want to know what i am missing here. I am running this program on eclipse. It is a simple program where i am passing reference of a and b to swap function and swapping values of a and b.

#include <iostream>
#include <stdio.h>
#pragma pack(1)
using namespace std;
//void swap(int &,int &);

int main()
{

int num1,num2;
cout<<endl<<"Enter two nos\n";
cin>>num1>>num2;
cout<<"\nBefore call\t"<<num1<<"\t"<<num2<<endl;
swap(num1,num2);
cout<<"\nAfter call\t"<<num1<<"\t"<<num2<<endl;
}
void swap(int &a,int &b) //Saving memory here
{
int c=a;
a=b;
b=c;
}
Asim
  • 413
  • 1
  • 4
  • 13

2 Answers2

2

The explanation is that one of the headers you have included is providing something named swap().

Some vendors are known for providing a swap() macro in stdio.h. The problem with that is it causes code to break when built with another compiler.

The other possible explanation is that the C++ standard provides a templated swap() function in namespace std, specified in the standard header algorithm. With some implementations, standard header files include each other (or provide functions specified by each other). Similarly, however, code which relies on one standard header providing functions from another will often break if your build your code with a different compiler - because the standard does not require such things.

So try removing your definition of the swap(). If your code still compiles/links/runs, one of the above is the explanation - at least with your compiler and standard library. Another indicator would be to place an output statement in your swap() - if your code still compiles/links/runs, but does not produce output - then that is another indicator.

If you remove the #include <stdio.h> (you're not using anything standard from it) and your code stops working, that will point to which header is the cause.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Hi peter,As suggested by you i commented #include and also added added a output statement inside my swap(), When i executed the program the numbers were swapped but output statement was not there. So it is using the swap() from "namespace std". Also i noticed that when i used the declaration then my swap() was executed as i could see the output statement.All clear now. Thanks for the help – Asim Feb 05 '16 at 06:08
  • Hi peter, I have one more doubt. You said if i run it on another compiler the code might break. Just wanted to confirm that do different compilers from different vendors have different implementation of swap function?. Is that the reason why the code might break or i am getting it all wrong. – Asim Feb 05 '16 at 06:17
  • 1
    @Asim It isn't a matter of implementation, but whether the C++ headers you included (`iostream`) indirectly include the header where `std::swap` is declared (`utility` header, `algorithm` for older versions of C++`.) – juanchopanza Feb 05 '16 at 06:31
1
  • Because in c++ there is predefined function called swap.
  • If you remove your function defination then also your code will compile because compiler considered it as predefined function.
Sagar Patel
  • 864
  • 1
  • 11
  • 22
  • Yes you are right. It is using swap() from namespace std. Thanks for the help. – Asim Feb 05 '16 at 06:14
  • Maybe "pre-defined function" gives the impression the function is always available. It is a standard library function, declared in a standard library header. That header has to be included, even if it is indirectly. – juanchopanza Feb 05 '16 at 06:36