7 years, 8 months ago.

Multiple Functions

Hello all,

I am writing a large program and want to break it down in to multiple functions for easier reading and trouble shooting. the problem i am running into is the functions need to interact with each other. But if one function is above the other function the upper function cannot see the lower function. What is the proper way to address this issue? I have included a example code that causes the same error, and the error.

Thank you!

Error: Identifier "FunctionTwo" is undefined in "main.cpp", Line: 21, Col: 10

void FunctionOne() {
    
        FunctionTwo();
    
    }

void FunctionTwo() {
    
        while (1) {
            
            }
}

1 Answer

7 years, 8 months ago.

You will need to use a Function prototype which are common to the C language. Using this method, your error will be resolved.

https://en.wikipedia.org/wiki/Function_prototype

so apply before FunctionOne():

void FunctionTwo();

Accepted Answer

Perfect! That work great is there any adverse effect if i call out several functions as prototype?

Also when i am calling out prototype it needs to include the same variable for the function correct?

posted by Tyler Harris 20 Sep 2016

Hi Tyler. Excellent. No issues to list as many prototypes as required. During my days at the university, our prof demanded this style to alert the reader that these functions will follow somewhere in the code list. Makes perfect sense. The only warnings are of course common sense ones such as when you declare your prototype to be different than the actual routines which follow - this will raise the respective error. The prototypes will dominate the definition of what is to follow. Prototypes just make for better code reading. Have fun coding !

posted by Sanjiv Bhatia 20 Sep 2016

Thank you, I do agree with making it more readable.

posted by Tyler Harris 20 Sep 2016