Page 66 - Computer_Genius_v2.1_C7_flipbook
P. 66

Now, click on the A button in the Game Controls to view the output of last iteration of the for loop.
                  Notice that, when the index value is 3, the loop skip the current iteration and display the output of
                  other iterations of the while loop.
































                  Functions

                  Functions are reusable blocks of code that perform specific tasks. They help organize your code by
                  making it more efficient and easier to understand. You can define a function once and use it multiple
                  times, saving you time and making your code easier to read. They are regularly used when writing
                  code to increase the readability of code, and to allow for reuse of common actions.
                  Benefits of Using Functions

                     Reusability: Avoid writing the same code multiple times.
                     Modularity: Break down complex programs into smaller, manageable parts.
                     Organisation: Easier to locate and modify specific code sections.

                  Steps to Create a Function

                  To create a function follow the given steps:
                  1.  Open the Functions Block:
                     o  In the MakeCode Arcade editor, look for the “Functions” block category. It usually resides at
                       the bottom of the toolbox.
                  2.  Create a New Function:
                     o Drag and drop the “function” block onto your workspace.
                     o  Rename the function to something descriptive. For example, if the function will handle player
                       movement, you could name it “movePlayer”.

                  3.  Add Code to the Function:
                     o  Inside the function block, add the code that defines the function’s behaviour. This could involve
                       moving sprites, playing sounds, or any other actions you want to perform.


                       64    Computer Genius (V2.1)-VII
   61   62   63   64   65   66   67   68   69   70   71