What is "void" good for?

I read in my book that it's "no data". Void pointers can used on most variables etc. I'm not really sure on when the best times are to use void.
"void" says to the compiler that there is nothing to be returned to the caller.

(i.e. )

1
2
3
4
5
     int main()
      {
       }

     void MakeCurrent(int number1)  //Functions that are of type void can take arguments but DO NOT return any values to the caller. 



Does that help a little?
Yes thank you. When you mean nothing is returned, it means that no value value get's settled in for number1?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

//Hypothetically speaking

#include <iostream>

using namespace std;

void MakeCurrent(int number1);

int main()
{
     cout<<"Main is here it MUST return 0 if used in best practice\n";
     return 0;
}

void makeCurrent(int number1)
{
   int number2, total;

number1 = 5;
number2 = 5; 

total = number1 + number2; //equals 10

}


Of course this is totally redundant because the function data is never seen but you may have a more clear notion as to what void is used for....

Functions hold | void, float, int, bool, double, etc| values if they are called they MUST return they're designated value back.

Last edited on
closed account (3qX21hU5)
I believe he was talking about void pointers and not void functions. This is what the websites tutorial has to say about them.

void pointers
The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).

This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.

One of its uses may be to pass generic parameters to a function:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// increaser
#include <iostream>
using namespace std;

void increase (void* data, int psize)
{
  if ( psize == sizeof(char) )
  { char* pchar; pchar=(char*)data; ++(*pchar); }
  else if (psize == sizeof(int) )
  { int* pint; pint=(int*)data; ++(*pint); }
}

int main ()
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << endl;
  return 0;
}
y, 1603



sizeof is an operator integrated in the C++ language that returns the size in bytes of its parameter. For non-dynamic data types this value is a constant. Therefore, for example, sizeof(char) is 1, because char type is one byte long.
Not quite was I was shooting for but yes that too...
Thank you so much guys, that clears up a lot.
Topic archived. No new replies allowed.