A common challenge in programming is to make code simpler and more readable. Especially when dealing with complex projects, code tends to become long and difficult to understand. Therefore, code is broken down into more manageable ‘functions’, which make the code modular. Each part of the self-portrait is enclosed in its own function. This makes draw() neater and easier to organise and maintain. The ‘function’ feature makes it possible to modularise complex drawing programs.

The benefit of function is that it seals the code into small groups. Each group is independent and responsible for a specific task or operation. That is, each module does only one thing.

截屏2024-10-09 18.11.21.png

Split the code into each group

截屏2024-10-09 18.13.17.png

When drawing the pupil, we can create a generic drawPupil() function so that we can call the same function to draw it whether it's the left eye or the right eye, just by changing the parameters.

Summary

The use of function makes the code concise. It makes it easier for me to read and understand. Helps me to organise my projects better, especially when dealing with large and complex projects.