Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • BlocklyProp Functions and Multicore

BlocklyProp Functions and Multicore

Functions in BlocklyProp

Functions are blocks of code that you put together to complete a specific job. Think of a function like a single block that you make, so you can use it over and over again. Just like other blocks, you can put it in your program anywhere that you need it, and as many times as you want.

With BlocklyProp, making a function is easy.  Take a look at the example below:

 

To make a function, enclose a group of blocks with the define function block, and then give it a name. After that, in the FUNCTION menu you will see a new run function block with that name, to use in your program. In this example, when our BlocklyProp program gets to the run function “printAge” block, it jumps to the matching define function block and runs the code inside of it. Upon completion, it jumps back and the program continues on.

Functions are handy if you have to repeat the same code in a few different places in your program. Instead of using many blocks over and over, you can place those blocks in a function. Then just use one run function block wherever you need it in your program.

  • Make sure that you are logged into BlocklyProp, with the BlocklyProp client running, and your board connected and powered on.
  • Make a new project using the blocks shown above, and run it.
  • When the terminal opens, click in it and enter your age. You should see something similar in your Terminal:

 

Try This

  • Let’s add a new twist, and a new function, that will calculate in how many years the user will turn 100.
  • Save the project, then save it again under a new name.
  • Duplicate the entire printAge function, and rename it oldAge.
  • At the end of the original printAge function, set a new variable named countdown, and make it equal to 100 minus yourAge.
  • Update the oldAge function blocks to use the text and the countdown variable as shown below.
  • Add a run function block for oldAge at the end of the main program
  • Save and run the program. When will you turn 100?


Did you Know?

Blockly Variables are Global. Did you notice that the variable item countdown was defined right inside the run function “printAge” block? That did not prevent it from being used inside the oldAge function. In BlocklyProp, all variables are available for use by any other part of the program.  This is not always the case in some programming languages, as you might learn if you study C.


Your Turn

  • Add a new function that figures out what year the participant was born, and have that print right after printing the age.
  • Add a new function that figures out what year the participant will turn 100, and have that print last.

Printer-friendly version
A Bit About Multicore
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress