Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 111 - 2609_Logicode
P. 111

    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.

                     4.  Call the Function:
                        o   To use the function, you  need  to  “call”  it.  Drag  and drop a “call  movePlayer”  block  (or
                          whatever you named your function) where you want the function’s code to be executed.

                     FUNCTION ARGUMENTS AND PARAMETERS

                     A function is a block of code designed to perform a specific task. It can take inputs, process them,
                     and return an output. Functions help to organize code, make it reusable, and improve readability.
                     A function may accept different values in the form of:

                       Parameter: A variable defined in a function’s definition. It’s like a placeholder for a value that
                       will be passed to the function when it’s called.

                       Argument: The actual value passed to a function when it’s called. It fills in the placeholders
                       defined by the parameters.
                     Let us create a project that demonstrates the use of function block for calculating area of a rectangle
                     using parameters and arguments. Prepare the project in MakeCode Arcade as shown below:

                                                                                           Advanced MakeCode Arcade  109

| 106 107 108 109 110 111 112 113 114 115 116 |