Outputting a void.

Jan 3, 2012 at 11:14pm
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?

Jan 3, 2012 at 11:25pm
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 Jan 3, 2012 at 11:27pm
Jan 3, 2012 at 11:52pm
Thanks for such a fast response.

Can you go into detail on exactly what a starting address is?
Jan 4, 2012 at 12:09am
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.
Jan 4, 2012 at 12:12am
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.
Jan 4, 2012 at 12:15am
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.