Return Values
The function print_line from the previous exercise prints, and that's it. But what if you want to keep calculating with a result, for example to add up the areas of all the rooms in a flat? A printed value only shows up in the output, and the program can't work with it any further.
That is why functions can return a value. The statement return sends a value back to the place where the function was called. Before the name of the function, you then write the data type of the value it returns, instead of void:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main(void) {
int result = add(2, 3);
printf("%d\n", result);
return 0;
}This program prints:
5The call add(2, 3) runs the function, and the function returns 5. You can imagine that the call is replaced with the value it returned, so int result = add(2, 3); becomes int result = 5;. The value that a function gives back is called its return value, and its type, here int, is the return type. A function can return a double, a char or a bool too:
#include <stdio.h>
double average(int a, int b) {
return (a + b) / 2.0;
}
int main(void) {
printf("%.2f\n", average(7, 8));
printf("%.2f\n", average(3, 4));
return 0;
}This program prints:
7.50
3.50Notice the 2.0: with 2, it would be whole-number division, and average(3, 4) would return 3.00.
You can use a call anywhere you could write a value: print it right away, calculate with it or put it in a condition:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main(void) {
printf("%d\n", add(2, 3));
int total = add(2, 3) * 2;
printf("%d\n", total);
if (add(2, 3) > 4) {
printf("More than 4\n");
}
return 0;
}This program prints:
5
10
More than 4In printf("%d\n", add(2, 3)), the program first calls add(2, 3) and then prints the 5 that the function returned. In add(2, 3) * 2, the call becomes 5, and 5 * 2 is 10.
Now you also know what the int before main means: main returns a whole number too. It returns it to whoever started the program, and 0 means that the program ended fine. That is the job of return 0; at the end of main.
A function that isn't void has promised to return a value, so it must return one. Forgetting return is a common mistake. This function prints the sum, but it doesn't return it:
#include <stdio.h>
int add(int a, int b) {
printf("%d\n", a + b);
}
int main(void) {
int result = add(2, 3);
printf("%d\n", result);
return 0;
}The program doesn't compile:
main.c:5:1: error: non-void function does not return a value [-Werror,-Wreturn-type]
5 | }
| ^
1 error generated.The error message points to the } at the end of the function and says that a non-void function (a function that isn't void) does not return a value. Elsewhere, compilers often only warn about this; here it's an error, so you can't miss it. The fix is return a + b; in place of the printf.
Instructions
The program doesn't compile: the error call to undeclared function 'rectangle_area' says that the function doesn't exist yet. Write it in place of the comment: it has two parameters of type int, width and height, and returns width * height. The program then prints Kitchen: 12 and Bedroom: 20.
The flat also has a living room that measures 6 × 5 meters. At the end of main, before return 0;, create a variable living_room with its area, calculated with the function rectangle_area. Then print the area of the whole flat, kitchen + bedroom + living_room, with a label: Whole flat: 62.
A new floor costs 12.50 per square meter. Above main, write a function floor_price with a parameter area of type int that returns the price of the floor, area * 12.5, as a double. At the end of main, print the price of the floor for the whole flat with two decimal places: Floor: 775.00.
Start programming for free
5/12