in C Plus Plus by
When is the "void" keyword used in a function C Programming?

1 Answer

0 votes
by

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]; 
} 
...