Ask coding questions

← Back to all posts
2
WHY IS THIS NOT WORKING??? C++

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

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

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

1

@SPQR
is there anything i can do about it?

1

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

2

@ItsEmpCs alternatively, declare it first, like this:

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

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.

1

@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?

1

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.

1

@Zeune
it says undeclared identifier 'second'