Lego Robots: Lab 4: Functions

Functions in Computer Programs

Functions are sub-programs. A set of commands that you can re-use over and over.

Functions are an important way of creating abstractions-- of providing a single name for some detailed operations. Abstraction allows us to break big problems into smaller ones.

Here's the wikipedia definition of abstraction:


Abstraction is the process or result of generalization by reducing the information content of a concept or an observable phenomenon, typically in order to retain only information which is relevant for a particular purpose.

Example: Brushing your teeth

Mindstorms provides a way to create functions-- they're called 'My Blocks
'.

Creating and Using Blocks

1. Open some program and select a set of blocks within it. The select Edit | Make a New My Block
2. Give your function a name 'Rectangle' and a description.

3. Your blocks will show up in the Custom Palette (the third palette icon at the bottom-left of the block palette). The first icon in the Custom Palette is My Blocks-- you'll find your newly created functions there.

4. Drag one or more instances of your function onto any program. When a function block is performed, all operations within it are executed.

Problems

1. Write a 'My Block' that has the robot drive in a rectangle once. Save this 'My Block' as 'rectangle' in your custom palette.

2. Write another 'My Block' that does the same thing, but the robot moves backward. Save as backrectangle in your custom palette.

3. Write a program that uses your two function blocks to have the robot move in a rectangle forward, then reverse it's tracks.



Recent site activity