Scratch-vm: Return values for "My Blocks"

Created on 15 Apr 2018  路  11Comments  路  Source: LLK/scratch-vm

I've not found a ticket for "My Blocks" with a return value, but multiple discussions about it in the forums.

Personally (and I don't seem to be alone...) I consider learning about returning a value from a function an important skill for the young programmers.

I'm opening this ticket to make a relationship between discussion like this ones

and the code being worked on.

Expected Behavior

Here is a possible "visual" implementation among many that are possible (the simplest i could think of)

  • Inside of a "My block" the "Return value" option is automatically added to the available variables in "set [my variable v] to ()" (and eventually also in "change [my variable v] by()"
  • The "Return value" variable cannot be renamed.
  • The value of the "Return value" is by default an empty string (or any better "null" value)
  • The block can be inserted as a block (as it's currently the case) or as a value (each block will be listed twice in the blocks lists)

Actual Behavior

Currently, the programmer has to create a global variable, change it in the block and use it in the script. With no direct relationship between the block being run and the value being inserted.

Steps to Reproduce

In modern programming functions are getting more and more important.

I consider an important skill to be able to create blocks / functions that do one thing, do it well, and return a single value.

request

Most helpful comment

I've read #79 and I thought it could be about returning values... but from the language used, I really could not be sure my impression was right.

If you have the feeling that this ticket is superfluous, you're welcome to close it. At, least, now there is a clear link between the wish to have return values in the custom blocks and what might be implemented.

I just want to stress that it would be really important to me, to have return values in scratch 3.0.
There are a few thing I would wish to have in Scratch, but return values are only one I (and the people around me) cannot understand why it's not there.
(Well, to be honest, if Scratch 2.0 was not as good as is, the need for Flash would have killed it for us!)

I still don't understand what custom reporters are, but if there is a way to do it, please give "us" functions in Scratch 3.0!

All 11 comments

79 addresses this

I've read #79 and I thought it could be about returning values... but from the language used, I really could not be sure my impression was right.

If you have the feeling that this ticket is superfluous, you're welcome to close it. At, least, now there is a clear link between the wish to have return values in the custom blocks and what might be implemented.

I just want to stress that it would be really important to me, to have return values in scratch 3.0.
There are a few thing I would wish to have in Scratch, but return values are only one I (and the people around me) cannot understand why it's not there.
(Well, to be honest, if Scratch 2.0 was not as good as is, the need for Flash would have killed it for us!)

I still don't understand what custom reporters are, but if there is a way to do it, please give "us" functions in Scratch 3.0!

As we are using Scratch in higher educational levels like K1/K2 it would make sense to implement return values in custom blocks, because this is a very vital concept in programming.

I perfectly agree that adding return values, will be very VERY useful!

A "reporter" in Scratch is precisely a block that returns a value. These blocks can be placed into other blocks that take inputs. Custom reporters, then, would indeed be custom blocks that return values. As such this really is a duplicate of #79 but I appreciate bringing it up again to garner more attention :)

Actually, C-style variables can be useful as well. But I am not creating a task for it. That means adding a boolean type, which is either true or false. Nextly, it seems that the for loops of scratch are always for _ in ... type, I recommend adding a block which looks something like this:

for every item in ____: do
assign the item to [ ] (Cannot be removed)
end

which is like this:

for i in 1...10{}
for i in somearray[]{}

Plus, the break statement might be useful.
This can be used for something like this:

import random
a = []
b = []
for i in range(0,9):
    a[i] = random.randint(0,9)
while True:
    for i in range(0,9):
        b[i] = random.randint(0,9)
    if b == a:
        print("Correct, code is:"), print(a)
        break
    else:
       print("Incorrect")
        continue

A "reporter" in Scratch is precisely a block that returns a value. These blocks can be placed into other blocks that take inputs. Custom reporters, then, would indeed be custom blocks that return values. As such this really is a duplicate of #79 but I appreciate bringing it up again to garner more attention :)

I do not think so. I recommend adding a checkbox labeled "Block can return value", plus a return type, which should hopefully look like this:

  • [x] Block has a return value
    Type of value:
  • [x] Boolean
  • [x] Number(Int on platforms)
  • [x] text(String or char)
  • [x] List (Array of things)
    In the first case, it makes the block hexadecimal, in the second case to the fourth case, the block is round.
    @Kenny2github Please tell me what is a reporter, it seems that they do not exist.
    This is the best method I can think of implementing this feature and not making it into a visual version of C or any other language.

Actually, C-style variables can be useful as well. But I am not creating a task for it. That means adding a boolean type, which is either true or false. Nextly, it seems that the for loops of scratch are always for _ in ... type, I recommend adding a block which looks something like this:

I'm curious what this specifically has to do with the concept of giving custom blocks the ability to return values? Certainly your ideas would be useful, but they don't seem directly connected to return values, so it seems they'd be better discussed as separate issues.

Regarding reporters: "reporter" is a term for round- or hexagonal-shaped blocks, which are implemented as part of the program by dragging them into input slots in other blocks. Stuff like "x position", "not <>", and "touching mouse pointer?" are all reporters. When speaking of custom reporters, we're suggesting the option to create blocks in the "My blocks" category and use them as reporter-shaped. #1064 (this issue) and #79 are different approaches at implementing that, but they both have the same general goal of letting users define their own reporters and place them into input slots just like any built-in reporter.

Actually, C-style variables can be useful as well. But I am not creating a task for it. That means adding a boolean type, which is either true or false. Nextly, it seems that the for loops of scratch are always for _ in ... type, I recommend adding a block which looks something like this:

I'm curious what this specifically has to do with the concept of giving custom blocks the ability to return values? Certainly your ideas would be useful, but they don't seem directly connected to return values, so it seems they'd be better discussed as separate issues.

Regarding reporters: "reporter" is a term for round- or hexagonal-shaped blocks, which are implemented as part of the program by dragging them into input slots in other blocks. Stuff like "x position", "not <>", and "touching mouse pointer?" are all reporters. When speaking of _custom_ reporters, we're suggesting the option to create blocks in the "My blocks" category and use them as reporter-shaped. #1064 (this issue) and #79 are different approaches at implementing that, but they both have the same general goal of letting users define their own reporters and place them into input slots just like any built-in reporter.

Because the <> shape is mostly boolean operators and the round stuff is usually of the int, string, or array type. This is the best I can think of while not turning it into a "visual version of C or Swift", although I am thinking of an app which can generate Swift code from blocks.

Actually, C-style variables can be useful as well. But I am not creating a task for it. That means adding a boolean type, which is either true or false. Nextly, it seems that the for loops of scratch are always for _ in ... type, I recommend adding a block which looks something like this:

I'm curious what this specifically has to do with the concept of giving custom blocks the ability to return values? Certainly your ideas would be useful, but they don't seem directly connected to return values, so it seems they'd be better discussed as separate issues.

Because the <> shape is mostly boolean operators and the round stuff is usually of the int, string, or array type. This is the best I can think of while not turning it into a "visual version of C or Swift", although I am thinking of an app which can generate Swift code from blocks.

Some definitions, just to make sure, we're all on the same page:

  • A boolean variable (what you're proposing) is a user-created variable in the Variables (orange) palette that has the <angle> shape and can only be set to true or false.
  • A custom reporter (the topic of this issue) is an extension to the My Blocks (pink) palette that allows a user to create a custom procedure that returns a value. Currently, custom blocks can only have the block/stack shape, and this issue proposes that there should be custom blocks that have the (round) shape as well.

@thesenatorO5-2 you're right that these ideas are related. If boolean variables existed, it'd make sense to allow custom reporters to return boolean values as well. But most uses of boolean variables would probably not have anything to do with custom reporters.

@towerofnix is saying is that, while boolean variables might be useful, they're a separate idea from custom reporters. They're related, but they're independent enough from each-other that this issue isn't the place to have that conversation. For-in loops are also useful, but I don't see how they're related to custom reporters, so that conversation probably also belongs in another issue as well.

For people claiming this task

Firstly, a return value is suggested, and also different types of return values. Blocks with a bool return type should be of an angle type, and numbers and strings should be round.
Secondly, the for-in loop can be used for iterating over arrays or to use the value i in other scripts.
Thirdly, there is also a suggestion for a break statement.

Was this page helpful?
0 / 5 - 0 ratings