The code inside your function will not be executed automatically, but you can call your function like other commands that we already learned and used:
1
2
3
4
function nameOfYourFunction() {
// Your code
}
nameOfYourFunction(); // this command actually executes our function
Notice that we can call a function only if it has been defined before we are calling it and calling is another word for executing it. That is one reason why we write functions at the beginning of our code right after our variables.
Functions are very useful if you need to repeat your code. For example, create two or more emojis or creating the eyes of your emoji, if it has two or more eyes 😉.
Let us create a function for an eye of our emoji. When we call it twice because my emoji has two eyes, you will notice a problem.
1
2
3
4
5
6
7
8
9
10
function eye() {
fill(255, 255, 255);
stroke(0, 0, 0);
ellipse(50, 30, 30);
fill(0, 0, 0);
ellipse(50, 30, 10);
}
eye();
eye();
You can only see a single eye, even we called it twice
Calling eye()
two times will still draw only one eye, as it seems. Because we use the same position for both eyes that we want to draw now. To change that, we need to learn about parameters first.