Can I define multiple local variables?

I'm pretty confused about local variables. Why do I have to define them outside a "do" ? What if I want to define multiple ones ? Why can't I just define them (more than one of them) inside a procedure - seems to be the most natural way of thinking about local variables - without having to enclose the relevant code within the local variable definition ? I feel like I must be missing something ...

1 Like

Perhaps one of these will help . Yes, you can define more than one inside a procedure. Perhaps the first link illustrates that. Or you can use the Blue control on the initialize local name to - in (do)


This block is a mutator that allows you to create new variables that are only used in the procedure you run in the DO part of the block. This way all variables in this procedure will all start with the same value each time the procedure is run.


In addition to the previous post, here are examples of the sandwich structure you would need for value procedures using local variables, taking the max or sum of lists ...


That's only if you are using them in a value procedure, where you are going to return a result.

Thanks for those examples.

But even if I use the form that isn't for value procedures, it still has the "in" portion, and don't I have to stick any code that's going to use the local variable within that ? Why can't I just define a local variable, and it's in-scope within the entire procedure (without having to stick the code "in"-side the local variable definition ?

Then show what you are doing and what you are missing. Local variables are so constructed in ai2 that everything related to these variables must be in the "in" area. You can do anything you want with it. If you don't want to use blocks in the "in" area, use global variables.

When using the block coding method, you need to change the concept of thinking. Not everything works in AI2 like it does in other programming languages.

Maybe the history of AI2 will answer your question.

It was originally (and still) intended as a classroom teaching tool, built on simple concepts that could be taught individually in small bites.

Procedures are introduced to eliminate code replication and to encourage code reuse.

Local variables are introduced after global variables, to introduce the concept of locality of scope, which can be a factor both within procedures and also in long stretches of inline code.
They were not part of App Inventor at Day 1, if I remember that far back.

The concepts of local variables and procedures are orthogonal. You can have either without the other.

Adding local variables to procedure blocks would require a new feature other than the blue button that adds parameters to the procedure. That's a change to the architecture.

Again, I'm not part of the MIT team, and I am relying in the history of the tool.

P.S. This reminds me of a common saying:

Keep your friends close and your enemies closer - Sun Tzu
Keep your parameters close and your local variables closer - ABG

Ok, I think I get it now. Thanks for the explanations.