Defining Functions
In this lesson, you will learn to write your own functions. Let's start with a problem. A game prints the same message every time you finish a level. The program in the editor does it for two levels:
#include <stdio.h>
int main(void) {
printf("Level 1 finished\n");
printf("==========\n");
printf("Well done!\n");
printf("==========\n");
printf("Level 2 finished\n");
printf("==========\n");
printf("Well done!\n");
printf("==========\n");
return 0;
}It works, but the same three lines are there twice. A real game has a hundred levels. Imagine writing those lines a hundred times! And if you wanted to change the message, you would have to change it in a hundred places, and you would surely miss one.
Programmers try not to repeat code. They write the repeated part once, give it a name and then use it wherever they need it. Such a named piece of code is called a function. A good name also says what the code does, so the program is easier to read.
In fact, you have been writing one function since the first lesson: main. The line int main(void) { starts the definition of a function named main, and the statements between the curly braces are its body. When you run a program, it starts by running main. You define your own function in a similar way:
void greet(void) {
printf("Hello!\n");
printf("Have a nice day!\n");
}A function definition has these parts:
void(English for empty) is the return type. It says that the function gives no value back. It only does something: here, it prints two lines. Later in this lesson, you will write functions that do give a value back.- The name of the function, here
greet. The same rules apply as for variable names: letters, digits and_, no spaces, and upper and lower case matter. Names made of several words are written in snake_case, such asprint_banner. Choose a name that says what the function does. - Parentheses with
voidinside. Here,voidmeans that the function takes no values. You will soon find out what else can go in the parentheses. - The body of the function in curly braces
{ }, indented by 4 spaces like the body ofmain. It holds the statements that the function runs.
Write the function above main, not inside it. In C, a function can't be defined inside another function. If you write it in the body of main, the program doesn't compile, and the compiler reports error: function definition is not allowed here.
When you run the program with the new function, it still prints only what main prints. That's not a mistake! A definition only says what the function should do, like a recipe written down in a cookbook: nobody has cooked anything yet. In the next exercise, you will make the function run.
Instructions
Above main, in place of the comment, define a function void print_banner(void). Its body prints the three lines of the message: ==========, Well done! and ==========. Then run the program: it still prints the same as before, because nobody has called the function yet. That's how it should be.
Start programming for free
1/12