Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can a void variable be used for?

For example, if I made the variable:

void helloWorld;

What could I do with it? Since it represents nothing, at first I thought that nothing could be done with it. I am aware that functions use void for no return value, but do the variables have a purpose?

-- EDIT --

I have found out the answer. I am now aware that void variables are illegal in programming languages such as Java, C++ and C. Thanks for all of your answers!

like image 642
Taking1n1 Avatar asked Sep 09 '13 15:09

Taking1n1


People also ask

What is the use of void data type?

The void data type is typically used in the definition and prototyping of functions to indicate that either nothing is being passed in and/or nothing is being returned.

What are void variables?

We say that a variable is void if its symbol has an unassigned value cell (see Symbol Components). Under Emacs Lisp's default dynamic scoping rule (see Scoping Rules for Variable Bindings), the value cell stores the variable's current (local or global) value.

What is the use of void data type in C?

The void type, in several programming languages derived from C and Algol68, is the return type of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their output parameters.


1 Answers

void variables are invalid in C/C++ because the compiler can not determine their size. void is only valid as function argument list (takes no arguments) or return types (returns nothing).

There is void * which just means "any type pointer" and is a generic pointer type, but you are not allowed to dereference it.

like image 186
Sergey L. Avatar answered Oct 03 '22 14:10

Sergey L.