How made a 'function' in App Inventor?

How to make the equivalent of a FUNCTION on the App Inventor?
Where by function I mean a sequence of code, to which to provide one or more input parameters, and which returns a value
For example, having to have as answer the color value of a key by supplying a string ("RED ‘,’ YELLOW ‘,’ GREEN '…) as input parameter a classic function is of the type:
key_background = numeric_color_from_string (“GREEN”)
The most similar blocks seems to me the ‘procedure that returns values’
How would you do it?

1 Like

Hello Amorosik

In App Inventor that’s a Procedure. On your selected Procedure Block, click the little blue cog button to define input parameters.


Edit: I haven’t shown how to use a Procedure Block for your example since it looks like a homework question. If you want help with something you want your App to do but your efforts have failed, show us your Blocks.

There are a lot of free tutorials by MIT. See the links to them on my site, plus a collection of tips and tricks and example code snippets.

The following tutorials are from the ebook mentioned below.

How to construct a Procedure:

Defining Procedures

How to use conditional statements to determine what happens if a condition is true:

Defining Procedures

Both tutorials probably will help you make your ‘function’ amoroskik. Based on your description of your ‘function’, you probably will use the Procedure block that returns a result. Code some blocks, you probably will be fine. If not, post an image of what you coded and someone might comment.

Here are some resources to help you learn to use the AI2 tools A very good way to learn App Inventor is to read the free Inventor’s Manual here in the AI2 free online eBook … the links are at the bottom of the Web page. The book ‘teaches’ users how to program with AI2 blocks.

There is a free programming course here and the aia files for the projects in the book are here:

How to do a lot of basic things with App Inventor are described here: .

Also look here and here for more tutorials.

It took me a while to discover this.
There is a special sandwich you have to build to
make an AI2 function that does any work to build a result:

From the outside in, you need:
a value procedure block (with nipple on the left)
a local variable(s) block (with nipple on the left)
a do-result block (with nipple on the left)

These examples might help.