• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

void in C++

Status
Not open for further replies.
Where exactly?

void myFunc(int i) --> no returnType for your function, int i is parameter

int myFunc(void) --> int as returnType, no parameters needed ... but "(void)" can also just become "()"

void myFunc(void) / void myFunc() --> no returnType, no parameters

edit:

When you declare a pointer you usually define the type of the pointer. If you just write "void *pt;" the type will be generic.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
void in C++ and in C means "nothing". It can be used as return type, or as unnamed parameter.

C++:
void myFunc(void)
{
...
}

basically this function tells you that it doesnt accept anything as argument and does not return anything.

You can replace void with nothing in argument signature, but this only works in C++, because in C that means the function takes any number of any arguments. So this is the same as above(in C++)

C++:
void myFunc()
{
...
}
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
no, it means function main returns no value.

JASS:
int main(){
 // do something here
 return 0;
}

void main(){
 // do something here
}
 
so void is a variable,
What makes you think now it is a variable?

It's not a variable. It's like an indicator for a non-type.

int myFunc() --> returns an int type

void myFunc()
--> returns nothing (void just indicates here that no data type will be returned in the function)

Like in chobibo's example before.
 
Level 25
Joined
May 11, 2007
Messages
4,651
void main(void) //All C++ projects need a main function
{
function1();
}

void function1() //A function that doesn't return anything when called
{
cout<<"Hello!\n";
cout<<function2();
}

int function2() //Returns an int when called
{
return 2;
}

Output:
Hello!
2
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,197
void means an absence of type.

returning a void type means the function does not return anything.

a void pointer means a pointer to no defined type. Although void pointers themselves are valid types (they correspond to a memory address) they cannot be dereferenced due to void having no type information. They also cannot be manipulated like normal pointers as the compiler has no size information for the void type (cannot commute byte offset).

no, it means function main returns no value.
Incorrect. The main function of a program should return an integer which corresponds to the application exit status. It should also take an array of character pointers and a argument count. The arc count and argument array can optionally be left out, which is usually the case for embedded software since it becomes the entry point of the processor so those arguments are not useful.

Code:
int main(int argc, char *argv[]);

Next to as a return type, void is mostly used for generic pointers. This is best seen in standard C for generic data structures like lists where void pointers to elements are commonly used. When getting the element you get a void pointer out of the data structure interface and then type cast it to the appropriate type (which you should know based on what you put into the data structure). In C++ void pointers see considerably less use as virtual methods and templates should be used instead as they are considerably more type safe and flexible.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
void main(void) //All C++ projects need a main function
{
function1();
}

void function1() //A function that doesn't return anything when called
{
cout<<"Hello!\n";
cout<<function2();
}

int function2() //Returns an int when called
{
return 2;
}

Output:
Hello!
2

http://ideone.com/6StORy

that summed it up.

And I dont give a shit about the order of declaration in your code, but the first word is already wrong
 
void main(void) //All C++ projects need a main function
{
function1();
}

void function1() //A function that doesn't return anything when called
{
cout<<"Hello!\n";
cout<<function2();
}

int function2() //Returns an int when called
{
return 2;
}

Output:
Hello!
2

You should always stick with int main(){return 0;}.
Some compilers require that you explicitly define main as such (GNU GCC Compiler - Code::Blocks IDE).
At the other hand some compilers will allow you to define main as void function, but still in background use it as int (Visual Studio Compiler - Visual Studio 2013 IDE, for example).

Another reason why I mention those two is that for example, Visual Studio will terminate your program asap main return value, because, you actually write that, right. At the other hand Code::Blocks will insert system("pause") command just before return, as well as do few other things, but that's story for itself.

Finally to use those functions (function1, function2) you need to declare them above main, and ofc. #include <iostream> using namespace std;.

EDIT:

Oh, another thing, it's not true that all C++ projects require a main function.
If you want to create Dynamic-Link Library (well DLL file) and link your core C++ code with C# project it won't have main at all.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
to quote standard:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations shall
allow both
— a function of () returning int and
— a function of (int, pointer to pointer to char) returning int

source: N3690 standard draft §3.6.1.2

it is required that main returns int
 
Level 17
Joined
Jul 17, 2011
Messages
1,864
i think for the purpose that you are asking its just a function which does not require you to write
return something;
at any point in it.

return type of a function is always specified at the start: int main, float g() etc..
these two have to have a return type int and float whereas void does not void functions are usually used to just perform an action (like displaying something) and not to modify data.
 
Level 15
Joined
Oct 18, 2008
Messages
1,588
Well... If you guys haven't noticed yet, the OP is more or less clueless in programming - why do you start throwing all the unnecessary stuff at him?

To answer the OP, think of a function as a mathematical function machine:
Function_machine.png

Now take a function in C++:

Code:
int Add(int i, int j)

The input of the machine thus are two integers: i and j, both of them being integers.

The machine will do something - here it's probably:

Code:
return i + j;

However, you need to specify beforehand what comes out of the machine. In this case, you set the type of the output as an integer. Why do you need this?

Because in case of a more complicated function that's not even written by you, you won't know what's inside the machine. It may return an integer, or maybe a vector? The point is, to use such a function, you need to know what you'll receive when calling it.

Now after this, void means that there is no output. You just call the function, give it some input values; it does something with it, and that's it. That something might be updating a database or uploading data, or maybe just increasing your input values.

Now having said this, there are functions where you have to use what you're told: i.e. Main() should always return an integer.
 
Status
Not open for further replies.
Top