15 Functions

Learning Outcomes

  • Comprehend the concept of function blocks and their role in defining and executing functions within a program.
  • Demonstrate the ability to create and define functions using function blocks.
  • Apply the use of function blocks to call and execute functions in a program.
  • Recognize the benefits of using function blocks to optimize code by reducing redundancy and improving code efficiency.

What is a function?

Functions are a way to save time and energy by storing commonly used blocks of code in one named set. For a real-world example, when you’re drinking a glass of water, the entire task might look like this:

  • Find glass
  • Bring over to water source
  • Pour water into glass
  • Make sure water doesn’t overflow!
  • Drink water

But instead of saying all of that, you could just say you’re drinking water! Functions work the same way. Multiple lines of code can be included in one function that you name and set up to be used in your program.


Defining a function

To define a function, or explain what is going to be in it, you will need the ‘to do something’ block in the pink functions menu. Drag that to your workspace and then name your function in the white box. This should be something memorable that describes your function well. Next, drag the blocks you need into the function.

image

Try it now! Make a function that has the name “blink” that has Zumi go forward and then blink.

image


Calling a function

Once you define a function, you should see a block for it in the functions menu. To use it, drag it into your workspace and drop it to wherever you would like to see it in your program.

image


Defining functions with parameters

What if we want the function to do something different depending on the parameters we provide? Remember that you can change the number of seconds Zumi drives forward. You can also add parameters to your functions! In order to add parameters to your function in Blockly, click on the gear icon and attach an input block to the inputs block. The input can then be accessed from the variables tab.

image

 

image

Warning: If you plan on using your function later on, do not remove it from your workspace! Doing this will delete the entire function nd you will not be able to locate it in the functions menu.


Zumi Rectangle

Write a function that makes Zumi drive in a rectangle pattern using two parameters (one of the long sides and one for the short sides). Try using the function with different parameter values and see what kinds of rectangles you can come up with!

Once you’ve made your rectangle function, try comparing it to either of the solutions below! Remember, when you want to call a custom function make sure that you drag the function block onto your workspace after defining the function.

The first solution makes Zumi drive in a rectangle shape using right turns:

image

The second solution makes Zumi drive in a rectangle shape using left turns instead of right turns:

image


 

Review

In this lesson, you learned how to use the function blocks to define functions and call them in your program! The function block is helpful for when you have commonly-used code that repeats often, and can be used to make your code smaller and more efficient.


Test Your Knowledge


Review The Key Concepts


Solve The Problem

Scenario: You are working with a team to streamline your automated delivery process. Currently, Zumi has a different process for small, medium, and large deliveries. How would these processes differ?

Problem: Create a function that can be used for small, medium, and large deliveries.


Sample Solution

There are many different ways that we can solve the problem listed above. Please watch the following video to review a sample solution completed by Ontario Tech University Engineering Students.

Demo

Code


Common Errors

Errors can also occur while creating a solution to the problem. Please review the video below to view some of the common errors that can occur.

Common Errors Video: Coming Soon.


Connecting The Dots

License

Foundations of programming with Blockly and Zumi Copyright © by Brandon Carson. All Rights Reserved.

Share This Book