Why does my C++ function, only when it's placed after main(), not work?

In C language, the declaration int func(); means a function with an unspecified number of arguments of any type, returning a int.

In C++ language, the same declaration int func(); means a function without any arguments, returning a int.

And therefore, in C++, the definition of func with an argument of type int is an overload. For the compiler, it is a different function, which in the original code is not declared before use, so an error is emitted.

But in C, it would be perfectly legal.


int func();

and

int func(int x)

See the difference? The first one should be

int func(int x);

You told the compiler that func was a function with no arguments, then when you tried to call it with one argument the compiler said 'no matching function'.

Tags:

C++

Function

Main