When is it appropriate to use void functions?

I was wondering if someone could help explain to me when it is appropriate to use void as a function type. Additionally, is it something you can/should use in your main() function?

This is a case where it came up on a project I was working on as an example of where I have stumbled upon it.

Thanks for your time!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include <iostream>
using namespace std;

void swap_numbers( int&, int&); //Function prototype

void main()

{

int num1, num2; // Initializing num1 & num2 where user will input integer

cout << "Please enter two integers: ";
cin >> num1;
cin >>num2;

swap_numbers (num1, num2); // Calling the function swap_numbers
cout << "The numbers reversed: " 
	 << num1 << ' '<< num2 << endl; //Printing out the switched numbers

}

void swap_numbers (int & a, int & b)

{
    int x; // Using as a temp. place holder 
    x = a;
    a = b;
    b = x;
}
Yes, std::swap() and other swaps are good examples of functions that do not return a value. Other common examples: std::vector::push_back(), std::sort(), std::srand(), std::mutex::lock(), etc.

And no, main() can never return void, many compilers will reject your program because of that error.
Oh ok. I was a bit confused because a few of the prompt shells the teacher has given out during class include void main and just happen to be thinking about that for some odd reason. Thanks again!
Topic archived. No new replies allowed.