How to run other functions in a function

I am trying to make a timer program, and have it all set out. Just one problem, i can't get the other functions to work. I am completely new to C++, just a couple
of video tutorials and the internet.
example:
1
2
3
4
5
6
7
8
int main(){
    runcode(5)
}
void runcode(int choice){
    if(choice == 5){
        //code
    }
}

but the compiler is returning:
||=== Build: Release in Clock (compiler: GNU GCC Compiler) ===|
In function 'int main(int, char**)':|
30|error: 'keydetect' was not declared in this scope|
||In function 'void keydetect(int)':|
58|error: 'changetime' was not declared in this scope|
|In function 'void changetime(int)':|
74|error: 'flag' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
I think I have to declare a function before hand, but i can't remember. Could someone please help?
Try placing the runcode() function above main() rather than below it.

Compilers start at the top of a file and work downwards. In order for the compiler to know that runcode() exists before it's called in main, the it needs to have encountered runcode() previously.

It's also possible to use a forward declaration, where you declare the function up top but define it later on. This essentially tells the compiler that the function exists so it can be used, but we'll implement it somewhere below.

1
2
3
4
5
6
7
8
9
10
11
void runcode (int choice);   // forward declaration

int main()
{
    runcode(5);
}

void runcode(int choice)
{
    // implementation
}
Last edited on
Topic archived. No new replies allowed.