C++ why use void and how to use the .exe

I am using Code::Blocks with GNU GCC Compiler. My first question is why do i use void here instead of int. i used int and it worked but the tutorial i am using suggested i use void. Can somone explain to me what the operator void does 0_0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
int ReadNumber()
{
    using namespace std;
    cout << "Enter a number: ";
    int x;
    cin >> x;
    return x;
}
 
void WriteAnswer(int x)
{
    using namespace std;
    cout << "The answer is " << x << endl;
}
 
int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
}


I think you are speaking about function WriteAnswer. There is nothing useful to return from the function. So it is defined as having return type void that is it returns nothing.
In the main you see that you are simply calling this function to print the value of expression x + y.

WriteAnswer(x+y);

What would be the sense if it would return something? The author of the function decided that its task is only to output the result and nothing more.
Interesting thankyou for solving this. Having an increasingly difficult time learning c++ from 0 programming :P
Topic archived. No new replies allowed.