faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 5 people (80%) answered Yes
Recently 4 of 5 people (80%) answered Yes

Entry

How can I call functions/voids below the current one, it won't let me when compiling?

May 27th, 2003 02:51
Colin Thomsen, Wes DeMoney,


You need to forward declare the second (lower) function before it is 
called. In the example below, somefunction2 is forward declared using 
the function signature with no implementation. The implementation is 
then given after somefunction.
#include <iostream>
// forward declare
void somefunction2();
// call somefunction2 here without yet defining the implementation
void somefunction()
{
    std::cout << "somefunction" << std::endl;
    somefunction2();
}
// now define the implementation
void somefunction2()
{
    std::cout << "somefunction2" << std::endl;
}
int main()
{
    somefunction();
}
Note that most C++ programs are written using two files - the header 
and source file. The header file declares all functions, usually in a 
class and then the source file defines the implementation.
This way, the order of function calls in the implementation doesn't 
matter.