void function does not work in visual studio 2012

I am trying to add a void function in Visual Studio 2012 but it doesn't work, meanwhile in Codeblocks it does:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int welcome();

int main(){
    welcome();
    welcome();
    welcome();
}

int welcome(){
    cout << "Welcome!";
}


Any ideas?
What do you mean by "it doesn't work". Compile error, linker error, runtime error?

You have forgot put a return statement in the welcome() function.
Thanks, but I don't want to put the return. How can I do that?
In Codeblocks there is no need to add return 0;
If you turn on more warnings you will get a warning in Code::Blocks too.
warning: no return statement in function returning non-void [-Wreturn-type]

Here is what the standard says about leaving out the return statement.
ยง6.6.3/2
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.


Undefined behavior is something that you should always try to avoid. It means that there is no guarantees and anything can happen.

The main function is an exception. If you don't return anything from main() it will automatically return 0.

If you don't want welcome() to return anything you can change the return type to void and leave out the return statement. Now when I read what you wrote more carefully I think this is what you want.
Last edited on
void welcome();
Thanks guys, I got the answer.

@peter
exactly :) Thanks
Topic archived. No new replies allowed.