1 of 20

Foundations of Technology

2 of 20

Introduction to Computer Science Principles byDesign

Unit 1: Computer Science Principles byDesign

Learning Cycle2 : Using Variables and Functions (Microbit)

Foundations of Technology

2

3 of 20

Big Idea

In this presentation we will learn how to use variables and functions in programming the microbit to solve engineering problems.

Foundations of Technology

3

4 of 20

What is a variable?

  • In programming a variable is a name that refers to a value.
  • If we ask microbit to evaluate the value of a variable , it will produce the value that is currently linked to the variable.

Foundations of Technology

4

5 of 20

When we create a variable, we can:

  • Initialize the variable: assign an initial value,
  • add /subtract into or from the current value of the variable
  • show the current value of the variable
  • plug the variable in any mathematical expression
  • creating a variable

5

6 of 20

Initializing a Variable

6

7 of 20

Incrementing a Variable by 1

7

8 of 20

Showing/displaying the current value of the variable

8

9 of 20

Using variable in math operations

9

10 of 20

Your turn:

Create two variables that can be updated and the current values shown in the microbit display when a button is pressed.

10

11 of 20

Functions

  • A function is a command or set of commands that can be used in different part of the program.
  • A function is executed when it is called in any part of the program.
  • Built in functions
  • You can make your own function

Foundations of Technology

11

12 of 20

Examples of built-in function

12

13 of 20

The function block

Foundations of Technology

13

14 of 20

Naming the function

14

15 of 20

Naming a function

15

16 of 20

Functions must be called to be executed.

16

17 of 20

Functions can be with/without parameters (Input)

Function without parameter

Foundations of Technology

17

18 of 20

Functions can be with/without parameters (Input)

Function with parameter

Foundations of Technology

18

19 of 20

For movements, utilize the servo simulation

    • Use the “servo write” block from the “pins” palette.

Foundations of Technology

19

20 of 20

Your turn:

    • Create two functions with parameter (number inputs).
    • Each function will execute a unique move or display.

Foundations of Technology

20