Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VOID mean 'nothing' or 'anything'

Tags:

c++

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.

like image 297
slvi Avatar asked Jul 28 '12 17:07

slvi


People also ask

Does void mean nothing?

The word void means "completely empty space" according to the dictionary. This term, when used in programming, refers to a return of "nothing" - an "empty value" so to speak.

What do you mean void?

noun. an empty space; emptiness: He disappeared into the void. something experienced as a loss or privation: His death left a great void in her life. a gap or opening, as in a wall.

Can you return nothing in void?

The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.


2 Answers

In C++, void can mean different things in different contexts. For example, a void pointer means a pointer that can point to any object

int x = 0;
void *p = &x; // ok -- void pointers can point to any object.

But when void is used as the return type of a function, it means that the function doesn't return anything.

void f()
{
  cerr << "Hello there\n";
  // No return, since the function has a void return type.
}
like image 83
Vaughn Cato Avatar answered Sep 25 '22 14:09

Vaughn Cato


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 address 0000 0000 which can hold only 8 bits. The second byte is at 0000 0001. And so on until you reach the last byte which is at address FFFF 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 the 01101010 will be seen as the number 106 (6A in hexadecimal, which is written 0x6A), 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:

  1. You are telling the computer that you are willing to see the bits in the memory as integer values.
  2. You are asking to reserve enough number of bytes in the memory that can hold an int, and how much is that? Since the int is 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 addresses 00AA 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 a char will reserve only 1 byte since it is 8 bits, while a float will 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_name like int x means 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. So int *p and char *q will 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 char pointer to point to an int variable (like char *q; int x; q = &x;)? Theoretically you can do this pointer but there is a problem. As I said before an int will take 4 bytes in the memory, while a char will only take 1 byte. This means if an int is sitting in memory at address AA00 FF00 and you want to put another one next to it, the new one will sit on the address AA00 FF04 because the first int already reserved 4 bytes (from AA00 FF00 to AA00 FF03) and the next available memory location (address) will be at AA00 FF04 in this case. Still with me?

So a pointer declaration means 2 things:

  1. Reserve 32 bit in memory to put a memory address in it.
  2. Every time we say 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; now p is pointing to the memory address where c is located and p++ 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? void is something that means "nothing" or "no type". Having said that, lets inspect the void on variables and pointers:

Can you say void x just like you said int 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 say void x you are not specifying how to treat the bits nor are you saying how many do you need.

Can you say void *p just like you said int *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 the void pointer to increment in p++? 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 the void pointer 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 a void pointer 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 an int for example, there is no benefit of creating a void pointer, but if you know that you will need to point to different types, then here the void pointer becomes handy. Sure this is not a beginner level topic.

OK, so void means 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 and int function will return an int every time you call it. Here are some valid examples:

int Function1();
//...
//somewhere in the middle of your program
int x, y;
x = Function1(); //valid
y = ( 100 - Function1() ) / 2; //valid
Function1(); //also valid but the return is lost since it wasn't stored or displayed

When you say that a function is of type void then 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:

void Function2();
//...
//somewhere in the middle of your program
int x, y;
x = Function2(); //NOT valid, Function2 will not return anything to be stored in x
y = 100 + Function2(); //Not valid, Function 2 will not return anything to be added to 100
Function2(); //valid, the function will be called, and will do some work, then you will continue execution after it terminates.

Do we benefit from a void function? 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 named ResetScore() to do this job, resetting has nothing valuable to return to you after you call it, so probably you will do the function as void 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 void function and non-void functions we need to answer a question. If a function is not void and 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 have x = F1(); then x is the one who will catch the return of the function F1(). This concept does not work on void functions because they don't return anything, in other words void F2() then somewhere y = 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() and int main()? In C++ the main() function has to have a return value, so void main() is not allowed. But for other functions the difference is that the void ones will not return anything to the caller while the int ones do. Well, who calls the main function? The Operating System (OS) calls it. What does the OS benefit from having a return from a main function? 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 of main() 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 the main() function is the last thing in all the program (when you exit the program).

I hope this clarifies your question.

like image 27
antf Avatar answered Sep 26 '22 14:09

antf