Ask coding questions

← Back to all posts
WHY IS THIS NOT WORKING??? C++
ItsEmpCs (39)

int main() {
std::cout<<"why???";
second();
}
int second() {
std::cout << "NOOOOO";
}

Answered by SPQR (479) [earned 5 cycles]
View Answer
Commentshotnewtop
SPQR (479)

second() is declared after it is called in main(). The compiler doesn't know what second() is when it's called.

ItsEmpCs (39)

@SPQR
is there anything i can do about it?

SPQR (479)

@ItsEmpCs just put it before main and you should be good

SPQR (479)

@ItsEmpCs alternatively, declare it first, like this:

int second();
int main() {
std::cout<<"why???";
second();
}
second() {
std::cout << "NOOOOO";
}
Zeune (16)

https://repl.it/@Zeune/testing-project here is the project fixed

One thing I did change was the method type from int to void because void is used when we are not returning a value.

void literally means nothing. It is usually used as a function or method return type, when the function isn't supposed to return anything. In the second method you typed out, with the void keyword, it is setting a value, and nothing needs to be returned.

CPPStudent7308 (8)

@Zeune You mean to say I've been wasting my time making functions and always making them return 0? I could have been using void this whole time?

Zeune (16)

Code that is written into the browser should be using the code tag `

If you are having trouble figuring out the tag you can click the Markdown hyperlink to open a new tab with the different commands or you can link your project. As well as to please post the error.

ItsEmpCs (39)

@Zeune
it says undeclared identifier 'second'