Previous: 5.3 Scalar Data Types
Up: 5.3 Scalar Data Types
Next: 5.3.2 Enumeration
Previous Page: 5.3 Scalar Data Types
Next Page: 5.3.2 Enumeration

5.3.1 Data Type void

The data type void actually refers to an object that does not have a value of any type. We have already seen examples of its use when we have defined functions that return no value, i.e. functions which only print a message and have no value to return. Such a function is used for its side effect and not for its value. In the function declaration and definition, we have indicated that the function does not return a value by using the data type void to show an empty type, i.e. no value. Similarly, when a function has no formal parameters, the keyword void is used in the function prototype and header to signify that there is no information passed to the function.

Here is a simple program using a message printing function which takes a void parameter and returns type void:

/*   File: msg.c
     This program introduces data type void.
*/

void printmsg(void);

main() {

/* print a message */ printmsg(); }

/* Function prints a message. */ void printmsg(void) { printf("****HOME IS WHERE THE HEART IS****\n"); }

No parameters are required for the function, printmsg(), and it returns no value; it merely prints its message. In the function call in main(), parentheses must be used without any arguments. Observe that no return statement is present in printmsg(). When a function is called, the body is executed and, when the end of the body is reached, program control returns to the calling function. Such a return from a called function without a return statement is often called returning by falling off the end. There are times when it is necessary to return from a void function before the end of the body. In such case, a return statement, with an empty expression may be used to return nothing:
void printmsg(void)
{
     printf("****HOME IS WHERE THE HEART IS****\n");
     return;
}
A return statement can also be used elsewhere in the body to return control immediately to the calling function. Consider a function which prints the values of its arguments if they are all positive; otherwise it does nothing:
void func(int x, in y)
     {
          if (x <= 0 || y <= 0)
               return;
          printf("x = %d, y = %d\n", x, y);
     }
If either of the arguments is not positive, the function returns to the calling function. If it does not return, then it prints the values of the arguments.

The use of void for a function returning no value is not strictly necessary. We could declare the function as being type int (or any other type) and simply not return any value and never use the value of the function in an expression. However, the void declaration makes the nature of the function explicit to someone reading the code and may allow the compiler to generate more efficient object code.



Previous: 5.3 Scalar Data Types
Up: 5.3 Scalar Data Types
Next: 5.3.2 Enumeration
Previous Page: 5.3 Scalar Data Types
Next Page: 5.3.2 Enumeration

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:40:40 HST 1994