What are function calls and why should I care?

Imagine a mighty king. He can give orders to his underlings, sit back and wait for the orders to be executed. While not have to care exactly how the orders is being carried out.

A function call is a lot like that type of order. And the programmer is the king!

A function call is a way in programming to make something happen.

To call a function is to say do your stuff, function!

In most programming languages (and almost all programming languages used for making games), function calls is one of the main type of building block of a program.

Let's try it out


Type the following into the the box where it says Enter source code here:


Click Check source code.


Click Run!.

So what is happening here?

On running the program the functions paintGreen(), paintRed(), paintBlue() and paintGrey() will be called. One after the other, from the top. These functions paints the box in colors hinted by the name of the functions when called.

Note that you do not need to know all the steps that is taking place to paint the area in different colors. You only need to know that when you call, for example, paintRed() the area will be painted red.

As a exercise, rewrite the source code to make the color of the box alternate between green and red two times. Like this green, red, green, red.

When you feel that you understand the content of this lesson. You are perhaps ready for the following lessons:
What are arguments?
The Music Generator
How to add background music to your games