Procedures in App Inventor and how they relate to Functions

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

Have you ever built an app in App Inventor and found yourself repeating the same blocks multiple times? What if you could write the blocks just once and use them whenever needed? That’s where Procedures come in! Procedures in App Inventor are like functions in other programming languages. They help us organize our code, avoid repetition, and make our programs easier to understand.

Imagine if you had to brush your teeth manually—one tooth at a time, every single day! Wouldn’t it be easier if you had a magic brush that cleaned all your teeth at once? Well, procedures work just like that magic brush in coding! Instead of writing the same code over and over again, we create a procedure and call it whenever needed. Super cool, right?

In this blog, we will explore procedures in App Inventor, how to create them, use parameters, and call them properly.

What is a Procedure?

A Procedure is a set of instructions that perform a specific task. Instead of writing the same blocks again and again, you can create a procedure and call it whenever needed. Think of it like a recipe—once you have written the steps, you can follow them multiple times without rewriting them!

Types of Procedures in App Inventor

In App Inventor, there are two types of procedures:

  1. Procedure without a result (does something but doesn’t return a value)

  2. Procedure with a result (returns a value after execution)

Let’s understand them with examples!

1. Procedure Without a Result

A procedure without a result is useful when you want to perform an action without returning any value. For example, let’s create a procedure to increase the score in a game.

Steps to Create a Procedure Without a Result:

  1. Go to the Blocks section.

  2. Click on Procedures from the blocks menu.

  3. Drag the to procedure block.

  4. Rename it to increaseScore.

  5. Add blocks inside it to increase the score.

Example:

  • Suppose we have a variable score that stores the number of times a button has been clicked.

  • We create a procedure increaseScore that increases the score by 1 every time the button is clicked.

Procedure Without a Result

Now, whenever the button is clicked, we can simply call increaseScore instead of writing the blocks again!

2. Procedure With a Result

A procedure with a result is useful when you need to calculate and return a value. Let’s create a procedure that calculates the area of a rectangle.

Steps to Create a Procedure With a Result:

  1. Go to the Blocks section.

  2. Click on Procedures from the blocks menu.

  3. Drag the to procedure result block.

  4. Rename it to calculateArea.

  5. Click on the blue gear icon to add parameters for length and width.

  6. Use the multiplication block to multiply length and width.

  7. Use the result block to return the value.

Example:

If we want to find the area of a rectangle, we need to multiply the length by the width.

Procedure With a Result

Now, whenever we need to find the area of a rectangle, we just call calculateArea(length, width), and it will return the correct value!

How to Call a Procedure

Once a procedure is created, we need to call it when required.

  • For procedures without a result, use the call block to run the procedure.

  • For procedures with a result, use the call block inside another block that needs the result (e.g., setting a label’s text to the result).

Example of Calling Procedures:

  • Call increaseScore when a button is clicked to increase the score.

  • Set Label text to calculateArea(5, 10) to display the area of a rectangle.

Why Use Procedures?

  • Avoid Repetition: Write code once and reuse it multiple times.

  • Make Code Neat: Organizes blocks and makes debugging easier.

  • Faster Execution: Procedures run efficiently and improve performance.

Procedures in App Inventor are like functions in other programming languages. They help us avoid repeating the same code and make our programs more efficient. We learned about two types of procedures—with and without a result—and how to create and call them properly.

Next time you build an app in App Inventor, try using procedures to make your code cleaner and smarter!

× We're here to help!