You are here: Home / Topics / When is the Void Keyword used in a function in C programming?

When is the Void Keyword used in a function in C programming?

Filed under: C Programming on 2022-07-16 16:43:16

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"); 
	return;		// the function does "return", but no value is returned 
} 

Here a function is declared, and all functions have a return type. In this case, 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(dst); 
	const char* src_c = reinterpret_cast(src); 
	for (int i = 0; i < numBytes; ++i) 
		dst_c[i] = src_c[i]; 
} 
About Author:
M
Manisha Agrawal     View Profile
If you are spending your time with genius person like me. You are the happy soul.