Using a function before declaring it

Hi everyone, I'm a Java developer wanting to learn some C++. Now in Java you can call a method when you want, also before its declaration. Normally you can't do this in C++, but is there a way to tell the compiler to look for the called function in the whole file, instead of throwing an error?
1
2
3
4
5
6
7
8
9
10
11
12
13
void function(); // declare function before you  call it

int main()
{
function();

}

void function() // implementation
{
// ...

} 

Last edited on
Yes I know you usually do this in C++, but (IMHO) it would be more "clean" to write this
1
2
3
4
5
6
7
8
9
10
11
int main()
{
function();

}

void function()
{
// ...

} 

and then tell the compiler (eg. through a switch) to look for "function" when it finds it in "main". I'm quite sure there is no way to do this, but I cannot understand why... it doesn't seem very difficult to me
Last edited on
In C++ functions must be declared before they used. I'm sure there are reasons for this.

EDIT: In C this code is valid:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
function();

}

void function()
{
// ...

} 

This is one of the differences between C and C++
Last edited on
Topic archived. No new replies allowed.