(MAI) Procedures Lesson

Procedures

In programming, we want to reuse code over and over again to save time and be more efficient.  

A procedure is a sequence of blocks or code that accomplishes a specific task. In computer science, a procedure also might be called a function or a method.

App Inventor has Procedure Blocks

  • procedure do

  • procedure result

procedure do

code block:
0 to procedure do

Procedure names in an app must be unique. App Inventor will not let you define two procedures in the same app with the same name. You can rename a procedure at any time while you are building the app, by changing the label in the block. App Inventor will automatically rename the associated call blocks to match. The procedure do block is the same as a void method in Java. It will perform an action.

code block:
call procedure

procedure result

When you create a procedure, App Inventor automatically generates a call block and places it in the My Definitions drawer. You use the call block to invoke the procedure.

code block:
0 to procedure
result { }

The procedure result block is the same as a  procedure do block, but calling this procedure returns a result.  This is the same as a return method in Java.

code block:
call procedure

After creating this procedure, a call block that needs to be plugged in will be created. This is because the result from executing this procedure will be returned in that call block and the value will be passed on to whatever block is connected to the plug.

[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION