void and int question

While learning C++ i have seen a lot of example codes. But there is something i do not understand. Sometimes int is used to declare a function but other times void is used instead.

What is the difference between int and void, and where would i use them.
you need to read some books/tutorials - otherwise, it's like guessing the meaning of hieroglyphics

A: int and void can be used as the return value of a function - void means the function doesn't return anything
try xoax's tutorials: http://xoax.net/comp/cpp/console/

they are really good
Also a function can be made to return any previously defined data type, not just 'int' or 'void'. What you are probably seeing if you're a beginner is the assignment of a data type to "main(...)" in which case the proper standard is to always return an 'int'. It used to be acceptable to return 'void' from a programs entry point but modern operating systems are designed to expect something from your application so the practice is now discouraged.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void printText(string str)
{
     cout << str << endl;
}

string text()
{
    return "I'm a return value";
}

int main()
{
    cout << text() << endl;
    printText("I'm void");
}

There's a simple example of void & returning a value.
Topic archived. No new replies allowed.