Outputting a void.

Ok, so I just started programming today and I've been going through these tutorials. It goes into how you can output return values and all that, but then it also explains how if you were to output a void function, it shouldn't even compile. I went ahead and tried it out, and it came up with the following (included code as well):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>

void ReturnNothing()
{

}
 
int Return3()
{
    return 3;
}

int main( )
{
	using namespace std;
	cout << Return3() << endl;
	cout << Return3() + 5 << endl;
	cout << ReturnNothing << endl << endl;

	system("PAUSE");
	return 0;
}


Outputs:
Output 1: 3,
Output 2: 8,
Output 3: 00FE1262


Ok, so the question is, why did it compile and why is it that random combination of numbers and letters?

closed account (zb0S216C)
You cannot output the value of void because void is an unknown type. Therefore, std::cout doesn't know how to handle it. Also, returning void indicates that the function doesn't return anything.

What your code actually does is print the starting address of ReturnNothing().

Wazzak
Last edited on
Thanks for such a fast response.

Can you go into detail on exactly what a starting address is?
The function is instructions in memory. Memory locations are called addresses. The starting address is the address of the first piece of memory that the function is in.
Starting address is just the initial address point in memory for a given element. Basically, it's nothing you need to worry about when you're starting out, but if you want, you can play with pointers and references and look up an article on addresses in memory.
void function();

generally means you will not return any value. Mostly seen this used in returning a cout string statement.

so it would go like this:

void greeting()
{
cout << "Hello, what is your name?\n";
cin >> name;
cout << "Hello " << getline(cin, name) << "!\n";
}
Topic archived. No new replies allowed.