When you make a function, there's a couple of ingredients to the recipe:
- It needs a name, so you can call the function whenever you want it to run
- Define the code that runs when the function gets called
- Usually provide some parameters so the function can do slightly different things each time.
function fillCircle (x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
You can think of a function as a
"program within a program".
It's important to understand the code inside the function doesn't actually run until you call the function.
fillCircle(150, 150, 20);