tags:

views:

180

answers:

4

If I put main on the top of the source file and invoke some custom functions, it will tell me that those functions are not found, but if I put main on the bottom of the source file, it will work.

Why? Is it because the compiler parse from top to bottom and breaks at the definiteion of main?

+13  A: 

It has nothing to do with main. C++ compilers work top to bottom, period.

Anything that you reference needs to be declared previously. The same goes for variables. In your case, you could do

void foo();     // <-- Forward declaration, aka prototype

int main() {
   foo();
}

void foo() {
   // Here is your foo implementation.
}
EboMike
The standard terminology for that "forward declaration" (when you're forward-declaring a function) is a **prototype**, and *every* function should have one.
Ken Bloom
Short answer, just perfect. So good it makes me almost cry out of luck :')
Time Machine
This has a C++ flag, so you definitely should *not* say void main().
Alexandros Gezerlis
I think as an example, it does the trick.As Ken said - it's common practice to have a header file (.h) with all the prototypes, and the implementations in the .cpp file. Better yet, since we're talking C++, everything is in classes, and you don't have global functions.
EboMike
@EboMike no global functions?! So the main function should be I'm a class too??
Time Machine
@EboMike: First of all, this is C++, not Java, so everything does not have to be in classes. Second, look at this on void main: http://www2.research.att.com/~bs/bs_faq2.html#void-main
Alexandros Gezerlis
@Ken: it would be a prototype in C. In C++, it's just a declaration (and C++ doesn't have anything that corresponds to what C calls a function declaration).
Jerry Coffin
No, main is special. And it really depends on what you do. But global functions are what you will use C - for C++, it's /typically/ cleaner to use classes. Again, it's all about what you're doing.
EboMike
@EboMike: Pardon me French, bit that's bullshit. C++ is a __multi-paradigm__ language, and OO is but one paradigms it supports. Free functions can actually enhance encapsulation. Making everything a member function simply is an implementation artifact on languages which lack support for non-member functions.
sbi
@EboMike: [Where possible, prefer writing functions as nonmember nonfriends.](http://www.gotw.ca/gotw/084.htm)
James McNellis
@Ken: "prototype" is not really a concept in C++. There is only one way to declare a function, giving its full signature, and that is called a "declaration". C-style declarations, omitting the parameter types, are not valid, since they are incompatible with overloading.
Mike Seymour
@Mike, @Jerry: The C++0x draft refers to prototypes in numerous places, for example 3.3.2, 3.3.4, 17.5.1.4, 20.7.4.3 (par 6), 20.7.5. Additionally, wikipedia says that "A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, arity, argument types and return type," and that C function prototypes were originally taken from C++. The C99 draft N869 (6.7.5.3 examples 4 and 5), uses the term prototype to refer to function declaractions that specify the types of the parameters. **So I am right to call it a prototype in C++.**
Ken Bloom
@Ken: Sorry to continue the pedantry, which is getting rather tedious now. The C++ Standard does, for some bizarre reason, call the scope of names in a function declaration "function prototype scope", and uses the term informally in a few places. But the formal specification of the grammar doesn't define anything with the name "prototype". C does have separate concepts of "declaration" and "prototype", as you say, but that's irrelevant to a question about a different language. So call it a prototype if you like, but don't claim that it's standard C++ terminology.
Mike Seymour
A: 

You should be putting your functions signature into header files that are included before main().

Byron Whitlock
Well, not always.
anon
+4  A: 

Basically, yes. You have to define/declare a function before you can use it.

anon
A: 

If main is declared before your custom functions, by the time main gets parsed it hasn't 'seen' any definitions for them.

Eton B.