Why is my HelloWorld function not declared in this scope?

You need to either declare or define the function before you can use it. Otherwise, it doesn't know that HelloWorld() exists as a function.

Add this before your main function:

void HelloWorld();

Alternatively, you can move the definition of HelloWorld() before your main():

#include <iostream>
using namespace std;

void HelloWorld()
{
  cout << "Hello, World" << endl;
}

int main()
{
  HelloWorld();
  return 0;
}

You must declare the function before you can use it:

#include <iostream>

using namespace std;

void HelloWorld();

int main()
{
    HelloWorld();
    return 0;
}

void HelloWorld()
{
    cout << "Hello, World" << endl;
}

or you can move the definition of HelloWorld() before main()


You need to forward declare HelloWorld() so main knows what it is. Like so:

#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
  HelloWorld();
  return 0;
}
void HelloWorld()
{
  cout << "Hello, World" << endl;
}

Tags:

C++

Scope