Definition Blocks

Definition Blocks

procedure

Collects a sequence of blocks together into a group. You can then use the sequence of blocks repeatedly by calling the procedure. If the procedure has arguments, you specify the arguments by using name blocks. 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.

When you create a new procedure block, App Inventor chooses a unique name automatically. You can click on the name and type to change it. 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.

procedureWithResult

Same as a procedure block, but calling the procedure returns a result. After the procedure executes, the result is returned to the block connected to the return socket.

name

Creates a named argument you can use when calling a procedure. You specify the argument by placing the name block into the procedure definition's arg socket. You can specify as many arguments as you like: each time you fill in an arg socket, another one will be created to accommodate the next argument. To name the argument, you click on the "name" label and type to change it.

When you specify procedure arguments, App Inventor will associate these with the call block generated for the procedure: the argument slots for the call block will show the argument names you specified.

For each name block you define, App Inventor creates an associated value block and places it in the My Definitions drawer. You use this block to refer to the value of the argument that is passed to the procedure when the procedure is called.

Note: In the current version of App Inventor, argument names in an app must be unique, even across procedures. For example you can't have two name procedures each with a variable named "x". When you try to create the second "x" block, App Inventor will not accept that name. This restriction will be relaxed in the future.

variable

Creates a value that can be changed while an app is running, and gives that value a name. Variables are global in scope, which means you can refer to them from any code in the app, including from within procedures.

When you create a new variable block, App Inventor chooses a unique name automatically. You can click on the name and type to change it. Variable names in an app must be unique. App Inventor will not let you define two variables in the same app with the same name.

When you create a variable, App Inventor will automatically create two associated blocks, and place them in the My Definitions drawer:

You can rename a variable at any time while you are building the app, and the associated blocks will be renamed automatically.

|

This block provides a "dummy socket" for fitting a block that has a plug on its left into a place where there is no socket, such as one of the sequence of blocks in the do part of a procedure or an if block. The block you fit in will be run, but its returned result will be ignored. This can be useful if you define a procedure that returns a result, but want to call it in a context that does not accept a result.