1.

When is the "void" keyword used in a function

Answer»

The keyword “void” is a data type that literally represents no data at all. The most obvious USE of this is a FUNCTION that RETURNS nothing:

void PrintHello() { PRINTF("Hello\n"); return; // the function does "return", but no value is returned }

Here we’ve declared a function, and all functions have a return type. In this case, we’ve said the return type is “void”, and that means, “no data at all” is returned. 
The other use for the void keyword is a void pointer. A void pointer points to the memory location where the data type is undefined at the time of variable definition. EVEN you can define a function of return type void* or void pointer meaning “at compile time we don’t know what it will return” Let’s see an example of that.

void MyMemCopy(void* dst, const void* src, int numBytes) { char* dst_c = reinterpret_cast<char*>(dst); const char* src_c = reinterpret_cast<const char*>(src); for (int i = 0; i < numBytes; ++i) dst_c[i] = src_c[i]; }


Discussion

No Comment Found