views:

341

answers:

5

I'm just starting to learn C++ so you'll have to bear with my ignorance. Is there a way to to declare functions such that they can be referenced without being written before the function that uses them. I'm working with one cpp file (not my decision) and my functions call themselves so there is not really a proper order to place them in. Any way to #define functions before they're used or something to that effect? Or maybe a way to tag them with a scope operator that doesn't entail them actually being part of a class?

Thanks in advance

+9  A: 

You can write function prototypes before you implement them. A function prototype names a function, its return type, and the type of its arguments. The only thing that needs to be above the call to your function is the prototype. Here's an example:

// prototype
int your_function(int an_argument);

// ...
// here you can write functions that call your_function()
// ...

// implementation of your_function()
int your_function(int an_argument) {
    return an_argument + 1;
}
yjerem
Thanks a lot. I knew it was something simple, but I wasn't having any luck googling it.
Chad
+2  A: 

Yes. Put the signature of the function at the top of the file, or in a header (.h) file.

So:

void OtherFunc(int a);

void SomeFunc()
{
    OtherFunc(123);
}

void OtherFunc(int a)
{
    ...
}
geofftnz
+4  A: 

I think what you are referring to is a function prototype.

This is where you define the prototype of a function in the header file, but define the implementation in a source (.cpp) file.

Source code that needs to reference the function simply include the header file, which gives enough information for the compiler to correlate the function call to the arguments and return value of the function you are calling.

Only at the linking stage does the function "symbol" get resolved against the source file -- if the function implementation didn't exist at this point, you would get an unresolved symbol.

Here's an example:

Library Header file - library.h

// This defines a prototype (or signature) of the function
void libFunction(int a);

Library Source (.cpp) file - library.cpp

// This defines the implementation (or internals) of the function
void libFunction(int a)
{
   // Function does something here...
}

Client code

#include "library.h"
void applicationFunction()
{
   // This function call gets resolved against the symbol at the linking stage
   libFunction();
}
LeopardSkinPillBoxHat
+2  A: 

Class member functions are declared in the header that defines the interface for the class. This header file should be included at or near the top of the CPP file that includes the implementation. So, it doesn't matter what order that the member functions are defined in the CPP file as all declarations have already been included.

From your question, I would imagine that you are considering writing free functions. You can use the same technique for declaring free functions; however, I caution against too many free functions.

Glenn
+4  A: 

What you need is a function declaration (aka prototype). A declaration is a function's return type, name and argument list without a body. These are typically in header files but they don't have to be. Here's an example:

#include< stdio >
using namespace std;

void bar( int x );  // declaration
void foo( int x );  // declaration

int main() {
    foo( 42 );      // use after declaration and before definition
    return 0;
}

void foo( int x ) { // definition
    bar( x );       // use after declaration and before definition
}

void bar( int x ) { // definition
    cout << x;
}
jwfearn