I am quite new to C++ and while trying to 'link' functions (methods?) together to make more interesting code. I found that you cannot call a function that is below the function you are calling it from.

example:

main()
{
"code"
run();
}

run()
{
"code"
}

This doesnt work. The compiler says it can't use an undeclared function.

Another example:

run()
{
"code"
}

main()
{
"code"
run();
}

This does work, I put it into laymans terms that it has 'seen' run() done its thing in main() and then been able to respond to me calling run() as it has 'seen' it.

I carried on writing the program, but now I have a problem 'run()' calls a function called 'command()'. But after 'command()' has done its thing it calls 'run()' meaning that the way I have been doing it they would both have to be above eachother, which is kind of a paradox.

So, I need a way of declaring that 'run()' exists right at the top of the src code. so 'command()' knows it is there somewhere and can call it.

Once I know how to fix this problem I can finish my program, please help!

Recommended Answers

All 2 Replies

You need to use function prototypes

void run(); // this is a function prototype
void command( const char* cmd); // another function prototype

int main()
{
    run();
}

void run()
{
    "code"
}

Thanks command() ran the call to run() perfectly. From now on I will declare my functions and put my code the right way around, having main() at the bottom was messing with my head anyway!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.