This keyword void is used when a function returns no value or accepts no parameters.
Here is an example of a function with void:
int main (void) {
return 0;
}
When a function does not return a value, then the keyword void must be prefixed to its declaration.
Here is an example of a function that displays an integer and returns nothing:
void println(int n) {
printf ("%d\n", n);
}
In this type of function, the return keyword can be used, but it is only used to end the execution of the function.
If a function accepts no arguments, you must specify void between the parentheses to inform the compiler (and the users)
that it does not receive any parameters. For example, the standard function rand() returns a pseudo-random number:
int rand(void);
Note that some compilers accept the omission of the void keyword between the parentheses.
However, it is preferable to write it systematically in order to ensure the portability of the code to another compiler.
Write a function printBase(unsigned int n, unsigned char base) which displays the integer n in the base base followed by a line feed:
n) is the number to display.base) is the base in which n will be displayed.The base parameter can take only three values:
n in octal base (format code %o)n in decimal basen in hexadecimal base We provide the beginning of the main program that asks the user to enter an integer. Complete this program so that the integer entered is displayed in the three bases:
Enter a positive integer: 185 In octal: 271 In decimal: 185 In hexadecimal: b9
#include <stdio.h>
// Prototype
// Display the integer n in octal, decimal or hexadecimal
void printBase(unsigned int n, unsigned char base);
int main(void) {
// Ask for an integer
int x;
printf("Enter a positive integer: ");
scanf("%d", &x);
// Display the integer in the three bases
printBase(x, 8);
printBase(x, 10);
printBase(x, 16);
return 0;
}
// Display the integer n in octal, decimal or hexadecimal
void printBase(unsigned int n, unsigned char base) {
switch (base) {
case 8: printf("In octal: %o\n", n); break;
case 10: printf("In decimal: %u\n", n); break;
case 16: printf("In hexadecimal: %x\n", n); break;
}
}
Write a function input() that captures a value from the keyboard and returns the value.
The body of the input() function should contain only 3 lines:
value variablescanf().return Test the function with the following main program:
int main(void) {
float n1, n2;
// Ask the values
printf ("Enter a value: ");
n1 = input();
printf ("Enter another value: ");
n2 = input();
// Display the average
printf ("Average = %.2f\n", (n1+n2)/2);
return 0;
}
Enter a value: 12.4 Enter another value: 13.7 Average = 13.05
#include <stdio.h>
// Prototype
// Function that waits and returns a value entered on the keyboard
float input(void);
int main(void) {
float n1, n2;
// Ask the values
printf ("Enter a value: ");
n1 = input();
printf ("Enter another value: ");
n2 = input();
// Display the average
printf ("Average = %.2f\n", (n1+n2)/2);
return 0;
}
// Implementation
// Function that waits and returns a value entered on the keyboard
float input(void) {
float value;
scanf("%f", &value);
return value;
}
Which of these functions do not receive any parameters?
void or nothing between the parentheses
Try again...
Which of these functions do not return any parameters?
void must precede the function name to indicate that it does not return a value.
Try again...
Let's consider the following function, which statements are correct?
void update(void);
Which functions receive no input parameters and return 2?