What does VOID mean?Does it mean ‘nothing’ or it means ‘anything?
When searched in the dictionary,the answer is nothing.But some people say it means anything.
i am confused does it means the same as null or it has a unique meaning.
When I searched for it’s meaning on google I got results for void pointers and many things more which I was not able to understand,at last I am a class12 girl.
Let me give you some detailed explanation on
void, variable declarations, functions and pointers since you mentioned you are grade 12. Allow me to start with variable declarations since it will be more adequate for my explanation.First, what happens when you declare a variable? In other words, what happens when you say
int x? As you know, all variables reside in the memory. The memory is divided into blocks where each block is 8 bits wide. These blocks are called bytes are the smallest addressable unit of memory in most computer architectures. Every byte has a number and we usually use hexadecimal numbers to represent the memory address of each byte. So for example the first position in the memory is the address0000 0000which can hold only 8 bits. The second byte is at0000 0001. And so on until you reach the last byte which is at addressFFFF FFFF.The important thing here is that the bits inside memory blocks are 0’s and 1’s. An example is
01101010. What I am trying to say that 0’s and 1’s are neither integers, nor characters, nor decimal values. They are nothing by nature, it is YOU who will decide how you will look to them. If you decide to see them as integers then the01101010will be seen as the number 106 (6A in hexadecimal, which is written0x6A), while if you decide to see these bits as characters then the bits will be seen as the letter ‘j’.Going back to my question. What happens when you say
int x? There are many things that take place:int, and how much is that? Since theintis usually 32 bits (it could be more or less depending on the CPU, OS etc.) then you are asking to reserve 32 bits from the memory. And how many bytes is that? Since we already said that each memory block is 8 bits then you are asking to reserve 4 bytes of memory, and they will be consecutive (for example, you will reserve the addresses00AA F000,00AA F001,00AA F002,00AA F003). This means that different variable types will reserve different number of bytes in the memory depending on their size. So acharwill reserve only 1 byte since it is 8 bits, while afloatwill reserve 8 bytes since it requires 64 bits and so on. This is NOT the case in pointers, so please pay attention!Before I jump to pointers I would like to repeat the idea in a different way:
type variable_namelikeint xmeans you are going to treat the contents of the memory as integers and you will reserve 4 bytes. Changing the type will change the treatment of the bits.Now pointers. What happens when you say
int *p? It is totally different than a normal variable. A pointer is something that will tell you the place of something (the memory address of something). Based on the above explanation, each memory address is something like this hexadecimal number: AA00 F001. So a memory address is an 8 digit hexadecimal number which means it is a 32-bit number. So when you are creating a pointer you are saying that you want to treat the bits as a memory address (not an integer, not a character, not a decimal value etc.). Because of that, all pointer variables will reserve in the memory the same number of blocks which is 4 bytes (32 bits) since any memory address is 32 bits. Soint *pandchar *qwill both reserve in the memory 4 bytes because both will hold inside them a memory address which is 32 bit wide.Here starts the action. Theoretically speaking, if all types of pointers will reserve the same number of bits in the memory, why don’t we use a
charpointer to point to anintvariable (likechar *q; int x; q = &x;)? Theoretically you can do this pointer but there is a problem. As I said before anintwill take 4 bytes in the memory, while acharwill only take 1 byte. This means if anintis sitting in memory at addressAA00 FF00and you want to put another one next to it, the new one will sit on the addressAA00 FF04because the firstintalready reserved 4 bytes (fromAA00 FF00toAA00 FF03) and the next available memory location (address) will be atAA00 FF04in this case. Still with me?So a pointer declaration means 2 things:
p++we will jump in memory over a number of blocks that is equivalent to the size (in bytes) of the type of the pointer and not related to the type of the thing you are pointing to. Here is an example:char c; int *p; p = &c;nowpis pointing to the memory address wherecis located andp++will not point to the next memory address, it will point to an address located 4 bytes away from the initial position, because the size of one step for an int pointer is 4 bytes.So what is
void?voidis something that means “nothing” or “no type”. Having said that, lets inspect the void on variables and pointers:Can you say
void xjust like you saidint x? No you can not, because in variable declarations you are specifying how to treat some bits and how many do you need. When you sayvoid xyou are not specifying how to treat the bits nor are you saying how many do you need.Can you say
void *pjust like you saidint *p? Yes you can because you are saying that you want a pointer, and knowing it is a pointer we directly know that you want 32 bits in the memory and we also know that you want to treat them as a memory address, so no ambiguity here like the previous case. OK, what happens when we tell thevoidpointer to increment inp++? Here is the problem. When the pointer type was known, then jumping in the memory was known but here it is not known. What is the solution? You need cast thevoidpointer later on so you can know how many blocks can you jump in the memory every time you increment. Well, what benefit do we get if at the end we will cast to something? Why don’t we create it directly in the type of that thing from the beginning? Usually avoidpointer is created where you will use it to point to different types every time, not only one type. If in a certain application you already know that you will always point to anintfor example, there is no benefit of creating avoidpointer, but if you know that you will need to point to different types, then here thevoidpointer becomes handy. Sure this is not a beginner level topic.OK, so
voidmeans nothing, what has it to do with functions? As you know the type of the function is a definition for what the function returns. So andintfunction will return anintevery time you call it. Here are some valid examples:When you say that a function is of type
voidthen you are saying that it doesn’t return anything when you call it (which is valid in C++). Here is what you can do and what you can not do with it:Do we benefit from a
voidfunction? Yes a lot, not every function returns something to us. Say you want are writing a program for a game that has a scoreboard. When a new game starts you want to reset the score to 0. Assume you have a function namedResetScore()to do this job, resetting has nothing valuable to return to you after you call it, so probably you will do the function asvoid ResetScore(). If you needed to have some feedback from that function then you might start to think of a different type.I hope this clarifies things for you. Again I wrote a detailed answer since you said you are grade 12. Good luck!
EDIT: Answer to your question in the comment below:
Before we understand the difference between a
voidfunction and non-void functions we need to answer a question. If a function is notvoidand whenever we call it, it will return something, then, who will catch the return of the function?”Simple. The one who catches the return is the one who called. For example if we have a function
int F1()and somewhere in the middle of your program you havex = F1();thenxis the one who will catch the return of the functionF1(). This concept does not work onvoidfunctions because they don’t return anything, in other wordsvoid F2()then somewherey = F2();will not work since F2 does not return anything and therefore we can’t use it in the same way we used F1.So the main rule is the one who calls is the one who catches the function return. In that case, what is the difference between
void main()andint main()? In C++ the main() function has to have a return value, sovoid main()is not allowed. But for other functions the difference is that thevoidones will not return anything to the caller while theintones do. Well, who calls themainfunction? The Operating System (OS) calls it. What does the OS benefit from having a return from amainfunction? It benefits to know if a program terminated normally or had abnormal termination, so in abnormal termination the OS can show you the well known “Send/Don’t Send” report message (talking about Microsoft Windows here). So the return type ofmain()will have no effect on your program or its output, it will only affect how it terminates because the return of the function is the last thing that happens in the function, and the return of themain()function is the last thing in all the program (when you exit the program).I hope this clarifies your question.