Hello World! I'm talking about functions in Alice3. One of the things that we need to do fairly often is to move an actor next to something. I've set up a simple scene with yeti, a fir tree trunk and an iceberg. I'm going to have the yeti turn to face the iceberg, and then I'm going to have him move to the iceberg. and when I run it you'll see that the Yeti is embedded in the iceberg. and the reason is because that command moveTo the iceberg took the point of the yeti and aligned it with the pivot point of the iceberg. so yeti is right smack dab in the middle of the iceberg. I'm going to add a variable. It will be a double, call it distanceToMove and give it an initial value of anything. I'm going to use a function, The procedures do an action; functions give an answer. So the distanceToMove is going to be getDistanceTo the iceberg. and instead of moving to the iceberg, I'm going to have the yeti move toward the iceberg some amount and then that amount is going to be distanceToMove. This is going to do exactly the same thing as that statement. Let's run it. He's still embedded in the iceberg. What I need to do is change the value of that. He needs to move that distance minus the width of the iceberg and minus his width. So I'm going to assign a new value to distanceToMove = distanceToMove minus (Let's move this over so you can see what I'm doing.) distanceToMove minus (We have to put in something.) -.25 but instead of that .25, I want the icebergs width and that's a function. and replace that .25 with that. and then, what I also need to do, another little bit of math here, and divide that by two, and let's run it. That's not too bad, but you can see he's still a little bit embedded in the iceberg, because here's his pivot, His point is right next to that. so I'm going to subtract half of that width also. Copy this to the clipboard. Do that and so I want this to be the yeti 's width. So, I have originally the distance was the whole thing from the yeti to the iceberg. which would embed him right smack in the middle. Then I subtracted halt the width of the iceberg, half the width of the yeti, and now can we run it Yeti moves right next to the iceberg. What I want to do is turn this into a function that will find the distance from any biped to any object. In order to copy this whole thing to the clipboard going to put it in a while loop, so we have this one, and this one, and this one are the parts that calculate the distance. I'm going to copy that whole thing to the clipboard and get a biped, add a biped function called distanceNextTo (to use a very descriptive name for that,) and copy from the clipboard, and you'll see that the yeti is in the red: change that to this. and for the iceberg I want to have some object so I need to add that object as a parameter that is going to be a gallery class and it has to be something that we can get the width of. We don't have a width for that one, let's go to the next one down and pick an SModel , an SModel does have a width. so I'm going to pick that and say OK, and rename this object. Oops, name that object, OK. That's now a parameter, change that object, and that to object and then I'm going to dissolve the while loop, just take that out of there. and we've got this big warning that says a return statement is required and we're going to return as the answer to the question how far should the yeti move to the iceberg, that's distanceToMove. Now let's go back to myFirstMethod. I'm going to delete this: it doesn't like distanceToMove So let's make a variable, name it distance. give it some starting value and then change that to distance but instead of that being a .25, I'm going to select my yeti, and you'll see that we now have a choice of this function, We'll put that right there, and the object we want to move to is the iceberg, and we run it and yeti moves to the iceberg. Let's try that after he moves to the iceberg, Let's copy this to the clipboard and have him turned to face the fir tree trunk. I can't copy this statement. I need to do an assignment statement for that, so, we're going to say distance equals something and then we're going to pick distanceNextTo the fir tree. And I can copy that to the clipboard. and just change this to the fir tree trunk. And we run it. He goes over there, turns around, and he goes next to the tree trunk. It doesn't he's next to it because the tree is all of that area, that's the whole tree, is that width. So he is next to the tree. You may have to fiddle with that a little bit. but we could add other objects, and we have this method and we could even use that function inside a procedure called moveNextTo something. And that's it