Block Variable defaulting to 0

I am creating a custom block and attaching a block variable.

The goal is to use the block to append letters into a variable. When inside a loop, I can then do this multiple times to build up a word backwards etc.

However, the block variable seems to be defaulting to "0" rather than empty. For example, if I input the word "hello" my output is "0olleh". I can't seem to define the block variables starting value.

Is there any way to do this?

You can start with

if (var = 0) [set (var) to ()]

A kludge, yes.

Are you related to Peter Noone? :~)

Hmm. That's true, block variables are initialized to zero. I wonder why that is. @jens, is there a reason?

On another note, are you sure you need a block variable for this? Can you share the project (or a screenshot) so we can take a look?

It's a misfeature inherited from Scratch -- everything is initialized to 0. :~)

Very true. But we also agreed agains null or nil or whatever special empty nothingness might describe a missing value :smirk:

I might be misunderstanding the term "block variable" but if I'm not, then it's either a regular variable or a scripted variable for block definitions. I tested both, and they work fine for me. My project has a custom block made to reverse a string you input. It works fine with scripted variables, and if you want I can rewrite the code to have a regular global (or local) variable. Either way doesn't add a 0 in front of the phrase.

@codeman1044 Yes, we use "block variable" as a technical term. @jens we should actually change the name to "static variable" which will be clearer to people who know what that means and less likely to be tripped over as in this conversation.

For the purposes of this discussion it doesn't really matter, because all variables are initialized to 0. So you have to SET the variable to an empty string.

Since you say in your sig that you want to learn the parts you aren't yet familiar with, I'm going to take the liberty of rewriting your code:

The less important part of the change is that this one uses the FOR block to reduce the size of the code. More crucially, this is a reporter, and there's no ask-answer to choose the word. This allows the block to be part of a composition of functions:


That makes more sense. I just misunderstood the question.
I don't see if it matters or not whether variables are initialized to 0, as you would still have to reset it to an empty value if you wanted to rerun the code. I could be wrong though.
Glad to know that custom blocks can become reporters! That is going to be useful.

This is how script variables are different from global variables and from (technical term, remember) block variables. Every time you call the REVERSE function you get a brand new, shiny variable (initialized to zero, sigh). This is important if, for example, you have three sprites and they all call REVERSE at once, with different inputs. Each will have its own RESULT variable, so they don't interfere with each other.

Ah... yes, you have a point. I guess I was only thinking about using global values when I posted.