What does the return 0 block does


What does it do?

It´s only for functions to return a value.

It allows you to return a value from your functions.
So if you were to write a function to get the distance between to sprites, you can return the distance instated of setting a global variable to the distance. It makes your code cleaner

Sorry. I don’t understand

What value do you return

You can return any number you want.

Does it means call the function how many times??

yeah is that what it means?

No, you make a function like squareroot. If you call squareroot with the parameter 9 you will return 3.

Functions are designed to wrap up everything needed to do a specific thing, such as spawning a enemy, or calculating the distance between 2 sprites.

You already know that you can pass parameters to functions, like the X and Y of where you want the sprite to spawn or the 2 sprites you want to get the distance from.

Now say you wrote a function to calculate the distance between 2 sprites. You can pass in the 2 sprites as parameters. If you use the return block, the function will exit and return the value you pass in. (The default is 0, as you can see) instead of setting a global variable to the result and reading it (which is very messy and not expandable), you can return it, like how you can pass in a number to the square root function and it returns the square root of the number you pass in.

You see how the square root function is a oval, not a block? That’s because it returns a value, and you can use that to set a variable, or pass it directly to another function!

I’m only in eighth grade. This is so hard to understand

I am too.

Don’t worry, you’ll get it :wink:

Look at this project:

Open the code.

Notice how there are 3 functions that preform the exact same thing. This is the Euclidean distance function. It’s used to calculate the distance between 2 points on a grid.

calculate_distamce_1 is function that takes the x and y of the Apple and the Burger sprite, feeds the values into the formula, and assigns the result to the Distance variable. It’s very inflexible, because it can only calculate the distance between the Apple and the Burger sprite and has to use a global variable to pass the output to the caller, which could easily lead to bugs in your code.

calculate_distance_2 is the exact same function as calculate_distance_1, except you can pass in the sprites you want to calculate the distance between them. This function is much more flexible than the last one because you can run this function with any 2 sprites, making the function more useful. But, it’s still using the same global variable Distance to pass the result back, which means you can’t use that variable again until you are done with it.

calculate_distance_3 is the best function out of all of them. It has the ability to allow you to choose whichever sprites you want to calculate the distance between AND it uses the return block, so the function turns into an oval, which means it returns a value, It doesn’t need any global variables except the 2 sprites you are passing in.

Do you understand?


The distance is
the square root of (110-37 ** 2) + 75-15 = that really long decimal

The second distance is equal to the first one.
Distance is equal to the (square root 110-37 ** 2) + 75-15 ** 2 = other really long decimal I said before
Then you return the value that you have and just use those numbers again I guess

I hope I said that correctly.
Thx @UnsignedArduino
Still a little bit confused so I hope I didn’t do all this for nothing

Umm…i don’t think that’s what I was intending to get across.

Here’s a part from Programming Arduino - Getting Started with Sketches by Simon Monk. Yes, it’s Arduino, but the explanation on functions is really good.

What Is a Function?
A function is a little like a program within a program. You can use it to wrap up some little thing that you want to do. A function that you define can be called from anywhere in your sketch (In this case, your code, but I’m just directly quoting him) and contain its own variables and its own list of commands. When the commands have been run, execution returns to the point just after wherever it was in the code that called the function.

When dividing your sketch and up into functions it often were thinking about what service a function could provide in the case of flash (In our case, calculate_distance_2) this is fairly obvious but this time let’s give this function parameters to tell it both how many times to flash and how short or long the flashes should be. (In our case, the 2 sprites we are passing in)

Return Values
Computer science, as an academic discipline, has its parents mathematics and engineering. This heritage lingers on in many of the names associated with programming. The word function is itself a mathematical term. In mathematics the input to the function (the argument) completely determines the output. We have written functions that take an input, but none that give us back a value.

Let’s take a look at writing a function that takes a temperature in degrees Centigrade and returns the equivalent in degrees Fahrenheit:

// C++
int centToFaren(int c):
    int f = c * 9 / 5 + 32;
    return f;
// Typescript
// I suck at TS so this might be wrong
function centToFaren(c: number) {
    let f: number = c * 9 / 5 + 32
    return f
# Python 3
def centToFaren(c: int):
    f = c * 9 / 5 + 32
    return f


The function definition now starts with int rather than void to indicate that the function will return and int to whatever calls it. (In our case, the function is now an oval, rather than a block, to indicate that it returns something) This might be a bit of code that looks like this:

// C++
int pleasantTemp = centToFaren(20);
// Typescript
// I suck at this so I might be wrong
let pleasantTemp: number = centToFaren(20)
# Python 3
pleasantTemp = centToFaren(20)


Did I make it more confusing?

Kinda but not really. I know about functions but never knew what the text or boolean or sprite (or anything else in that area) when you make a function. I don’t know python or javascript or typescript. I’m into mainly blocks.

I’m gonna pretend that c=2
f is equal to 55 then return f is return 55

And the A button turns on the function Call centToFaren

Wait a minute. you are changing farenheit to celcius

I defined a function in the post above to convert Centigrade to Fahrenheit. It makes use of the return statement in the return values section

Still confused but I know about temperature

I’m so sorry, I must be really confusing you.

1 Like