What is the difference between void and int?

I don't get the difference. I read some threads saying void returns nothing an int returns and interger but they both work the same.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int printCrap(int x){
    cout << "The number is " << x << endl;

}

int main(){

    printCrap(754);
    return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

void printCrap(int x){
    cout << "The number is " << x << endl;

}

int main(){

    printCrap(754);
    return 0;
}



They both work the same way and print out in the program "The number is 754"

Can someone explain to me in simple English?
They give the same output, since they both print the same statement that's in the cout statement.

They don't really work the same. The first version is designed to return an integer value back to the calling function, although there's no code that actually does the return. Your compiler might give you a warning about that. The compiler won't expect a function with return type void to return anything.

A way to use the function to return a value and then do the output in the main function would be something like this.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int printCrap(int x){
    return 2*x;
}

int main(){
    int x = 377;
    cout << "The number is " << printCrap(x) << endl;
    return 0;
}


Topic archived. No new replies allowed.