[Solved] How to call a C++ function in a C++ Program [duplicate]


The function program is not known to the compiler since it’s declared after your main function. You must declare it before like so

int program(); // Declares the function program

int main()
{
    program(); // function is declared, the compiler knows its return-type, name and parameters
    return 0;
}

// Here is the function definition
int program()
{
    // ....
}

A simple way to think when you’re new to C/C++ is that the compiler works through the files “from top to bottom”. This means you can’t use a variable/method x before it has been declared.

You must do the same with variables. A simple example

#include <iostream>

// Declare an integer variable called b. The use of the word
// extern means that the variable is just declared, not initialized
extern int b;

int main()
{
    // This will compile, you have declared b above so the compiler "knows" about b
    std::cout << "b = " << b << std::endl;
    return 0;
}

int b = 7; // define b

4

solved How to call a C++ function in a C++ Program [duplicate]