Voids

Apr 15, 2011 at 10:39pm
I was wondering if
1. Does a void basically just place code in from a different section and you can call it whenever you want?
2. Is it possible or is it ethical to do something like:
1
2
3
4
5
6
7
int main()
{
 one();
}
Void one{
Main();
{

3. Gotos are hated or just not used?
Apr 15, 2011 at 10:42pm
1. void means it does not return a value. Nothing more, nothing less. You can call any function at any time, and you are never forced to assign the value it returns to a variable. void just means you can't assign it to a variable, because there is nothing returned...it's like x = /*nothing*/;

2. No, because C++ is case sensitive and you wrote Main instead of main on line 6.

3. They are typically frowned upon, mostly because they don't care about scope and can cause issues when used wrongly.
Last edited on Apr 15, 2011 at 10:43pm
Apr 16, 2011 at 3:03am
The only thing you need to know about a void function is it doesn't return a value. You can still use it to set values using references though, for example...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void ChangeMyInt(int &myInt);
int main()
{
	int myInt = 10;

	cout << "myInt: " << myInt << endl; //Output = "myInt: 10"
	ChangeMyInt(myInt);
	cout << "myInt: " << myInt << endl; //Output = "myInt: 20"

	return 0;
}

void ChangeMyInt(int &myInt)
{
	myInt = 20;
}
Last edited on Apr 16, 2011 at 3:03am
Apr 16, 2011 at 3:08am
They are great when used with structures.
Topic archived. No new replies allowed.