In this section, we will learn what the void function is and how to use it in C.
Note: we’re assuming you’re already familiar with the functions in C.
What is void in C?
The void keyword in C is used as the return data type of a function when, in fact, the target function does not return a value.
Basically, the `void` keyword is a sign to the execution engine and developers that the target function won’t return a value and so the function shouldn’t be used in places where a value is excepted (for example as the argument of another function, or as the assignment to a variable, etc.).
Void Function in C: Syntax
This is how we create a void function.
void functionName();
As you can see, we use the `void` keyword right where the data-type of a function is used to declare that the function won’t return a value.
Example: declaring void function in C
#include <stdio.h> void printSum(int, int); int main() { printSum(100, 200); return 0; } void printSum(int val1, int val2){ int res = val1 + val2; printf("The result is: %d\n",res); }
Output:
The result is: 300
In this example, the `printSum()` function’s return type is set to void, which means the function does not return a value when we call it.
Note: when a function does not return a value (its data type is set to void), you should not use the `return` statement in that function anymore. If you do so, you’ll get a compile time error instead.