Chapter 21. Defining Procedures and Reusing Blocks

Figure 21-0.
Defining Procedures: Reusing Blocks

Programming languages such as App Inventor provide a base set of built-in functionality—in App Inventor’s case, a base set of blocks. Programming languages also provide a way to extend that functionality by adding new functions (blocks) to the language. In App Inventor, you do this by defining procedures—named sequences of blocks—that your app can call just as it calls App Inventor’s predefined blocks. As you’ll see in this chapter, being able to create such abstractions is very important for solving complex problems, which is the cornerstone of building truly compelling apps.

When parents tell their child, “Go brush your teeth before bed,” they really mean, “Take your toothbrush and toothpaste from the cabinet, squeeze out some toothpaste onto the brush, swivel the brush on each tooth for 10 seconds (ha!),” and so on. “Brush your teeth” is an abstraction: a recognizable name for a sequence of lower-level instructions. In this case, the parents are asking the child to perform the instructions that they’ve all agreed mean “brush your teeth.”

In programming, you can create such named sequences of instructions. Some programming languages call them functions or subprograms. In App Inventor, they’re called procedures. A procedure is a named sequence of blocks that you can call from any place in an app.

Figure 21-15 is ...

Get App Inventor 2, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.