2.5. Blocks that Return Values

Sometimes what we want a block to do does not result in something visible (like drawing on the screen), but the calculation of some piece of data that the rest of the program needs.

When a block needs to come up with an answer and pass that back to the location it was in, we call the block a Reporter. The special Report block is what we use to say “here is the answer produced by the block” (in other programming languages the keyword return is often used instead of report).

The video below introduces reporters:

Checkpoint Exercise

Add three values:

Make a block that takes in three numbers and report their sum (a 3 input + block)

../../_images/CP24a.gif

Note

You should NOT use Ask [ ] and wait block inside the add [ ], [ ] and [ ] block. The input to the block should come from the parameters (holes where a value can be placed).

You should NOT use Say [ ] block inside the add [ ], [ ] and [ ] block. Give the output with report [ ]

../../_images/CP24a_key.gif

Negate:

Make a block that takes in a number and negates it (report 0 - the input).

The picture below shows it being used with an input of 5. (The output should be -5)

../../_images/CP24b.gif

You should be able to use it as part of a complex math expression. Below it is shown with the result of a [ ] + [ ] as its input, and with its reported value being used in another block. The correct outputs would be -6 and -30.

../../_images/CP24b2.gif

Note

You should NOT use Ask [ ] and wait or Say [ ] blocks inside your block.

Input should come from parameters, output should be handled with report [ ]

You should not have to worry about HOW the block is being used as you write your code inside of it - your job inside the block is always the same: take the parameter (input value), subtract that from 0, report back your answer.
../../_images/CP24b_key.gif

Pythagorean:

Make a block that takes in two inputs a and b. It should treat them as the legs of a right triangle and calculate the length of the hypotenuse c using c = \(\sqrt{(a^2+b^2)}\).

../../_images/CP24c.gif


There is a square root block you can use to help do the math that looks like:

../../_images/CP24c2.png

Note

You should NOT use Ask [ ] and wait block inside the draw triangles block. The input to the block should come from the parameter (hole where a value can be placed).

This version uses two variables: aSquared and bSquared to store work as we go. You could do all the work in one set of nested blocks, but that makes it harder to debug when something goes wrong.

../../_images/CP24c_key.png