INTRODUCTION
A function is a block of code made up of a set of steps that results in a single specific action. The programmer will give this action a simple name. Giving a simple name to a function increases the chances that the set of steps can easily be talked about and reused again and again in the program.
As you can see in the flowchart, we have defined the Addition function which now you can use anywhere in the program.
FUNCTIONS IN PICTOBLOX
In PictoBlox, we execute functions with Custom Blocks.
ACTIVITY: FUNCTION WITH NO PARAMETER
Let’s create a block that sends a message from Tobi whenever the block is being executed.
Follow the steps below:
- Create a New file in PictoBlox App.
- Click on the button “Make a Block“ in My Blocks.
- A popup will open. Now you can enter the block name you want to give. Let’s make it “Send Message“. Click Ok when done.
- You will observe two things:
- First, a definition hat block is created in the scripting area. Then, you can add the script below the block, which will be executed whenever this block is called.
- A block below the “Make a Block” appears. You can use this block anywhere in the script.
- Add say () for 2 seconds block below the define (Display Message) block.
- Add the Display Message block below when green flag clicked block.
- Run the script and see what happens
ACTIVITY 2: FUNCTION WITH PARAMETER
Consider a problem statement, where we are required to calculate the area of a circle. To do so, we need to know the radius of the circle whose area we are required to calculate.
Area = 3.14 * Radius * Radius
However, the formula for calculating the area of a circle always remains the same.
So, in the above-mentioned scenarios, if we consider the task of calculating the area of a circle, then the formulae used to calculate the area can be considered as the body of the function. However, to get a concrete value from the functions, we need to provide the value of the radius in the case of calculating the area of a circle.
Thus, the variables accepted by a function to perform the set of tasks defined in its body are called function parameters.
TYPES OF ACCEPTABLE PARAMETERS IN PICTOBLOX
You can add the following parameter types in a function definition:
- Number
- Text
- Boolean
ADVANTAGES OF USING FUNCTIONS
A few of the advantages of using functions are:
- Increases in readability make code organized and easy to understand.
- Reduces code length: redundant code is removed and replaced by functions.
- Reusability: Code reusability increases.
CODE
Let’s create a program that asks the user to enter the radius of the circle and calculates the area and the perimeter of the circle.
The formula that we will use are:
- Area = 3.14 * Radius * Radius
- Perimeter = 2 * 3.14 * Radius
Follow the steps below:
- Create a New file in PictoBlox App.
- Create 2 blocks with Radius (Number Input) in PictoBlox.
- Area (Radius)
- Perimeter (Radius)
- Add the definition of the Area block.
- Add the definition of the Perimeter block.
- Create the main script.
- The script is complete. Click on the green flag to run and test it.
- Save the file as Functions.