What is a procedure?
A procedure is a set of instructions that perform a specific task or tasks. It may also be called a function. A recipe for banana bread is an example of a procedure. The baker must follow the instructions step-by-step to produce the bread.
We use procedures in App Inventor to create new blocks that we can use repeatedly and take up less space than all of the blocks used in the original procedure. If we are using the same sets of blocks more than once, these blocks are called redundant.
What is a procedure in App Inventor?
An App Inventor 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.
Let’s build a procedure to do the job of the redundant code blocks. In App Inventor, you define a procedure in a manner similar to how you define variables. From the Procedures drawer, drag out either a procedure do block or a to procedure return block. Use the latter if your procedure should calculate some value and return it. After dragging out a procedure block, you can change its name from the default “procedure” by clicking the word “procedure” and typing the new name.
In the MoleMash tutorial, you create a procedure called MoveMole to move the mole to a random location on the screen.
After you create a procedure, a block is put in the Built-In Procedures drawer that lets you call your procedure.
Additional details on App Inventor's procedure blocks can be found on the Procedure Blocks page.
What is an argument?
An argument is an input to our procedure. Some procedures require knowing some bits of information that change how the procedure is run. Let's look at the banana bread recipe example. Sometimes the baker may want to make two loaves of banana bread. Then the baker would double the amount of each ingredient called for in the recipe. This double or 2 is an argument to our procedure since this changes how much of our ingredients we mix together.
When you create a procedure, you can use the mutator button to add arguments.
By hovering your mouse over an argument, you will see a get and set block appear. Drag these blocks onto your screen to use them.
This procedure takes two arguments: x and y. Then it returns the result of adding x to y.
A procedure is a set of blocks together in a group that can be run by calling the procedure. A procedure can return something or not have a return value. A procedure can take arguments.