top of page

Block Programming - Functions

Functions are a fundamental concept in programming, including block programming, because they allow you to group a set of actions together that you want your program to perform. Once defined, these actions can be called upon and executed as many times as needed. This can greatly reduce redundancy in your program and make your blocks easier to read and manage.


Let's consider a block programming scenario involving a robot programmed to move in a square shape. Without a function, you'd have to drag and drop the move and turn blocks for each side of the square, resulting in the same set of blocks repeated four times.


  1. Creating a Function: In the function or procedures section of your block programming interface, you would create a new function. You could name this function "DrawSquare". Inside this function, you would place two blocks. First, the move block that makes the robot move forward a specific distance (let's say 10 units). Second, a turn block that makes the robot turn 90 degrees.

  2. Calling a Function: Once your function is defined, you can call it from anywhere in your program. You would do this by going to the functions section and dragging the "DrawSquare" block into your main program area.

  3. Using a Function in a Loop: To make the robot draw a complete square, you would need to call the "DrawSquare" function four times. Instead of manually placing four "DrawSquare" blocks in your main program, you could place a loop block (often labeled "repeat" in block programming languages) into your program and set it to repeat four times. Inside this loop block, you would place your "DrawSquare" function call block.

In this manner, your blocks could look something like this in text format:


def DrawSquare():

move_forward(10)

turn_right(90)


repeat 4 times:

DrawSquare()


Here's another example. Let's create a function that will make our robot follow a specific path when it detects an object.


1. Creating a Function: First, we'll create a function named "AvoidObstacle". Inside this function, we'll include blocks for a routine that we want the robot to perform when it detects an obstacle. This could be something like:

  • Stop moving.

  • Move backward a little.

  • Turn right 90 degrees.

  • Move forward again.


2. Creating an If Condition: Now, in the main program, we'll include a loop block that constantly checks whether the robot's front sensor detects an obstacle. This is done by placing an 'if' block inside a 'forever' or 'repeat until' loop.

  • If the condition (e.g., front sensor detects an obstacle) is true, then call the "AvoidObstacle" function.


Here's a pseudocode example of what the blocks might look like:

def AvoidObstacle():

stop_moving()

move_backward(2)

turn_right(90)

move_forward(5)


repeat until program ends:

if front_sensor_detects_obstacle():

AvoidObstacle()

else:

move_forward()


This way, the robot will constantly move forward until it detects an obstacle. When it does, it will perform the sequence of actions defined in the "AvoidObstacle" function, effectively maneuvering around the obstacle. After that, it will continue moving forward again.

Recent Posts

See All
Block Programming - Operators

Operators are another key concept in programming that you'll often use in block programming as well. They let you perform operations on...

 
 
Block Programming - Variables

Variables in block programming work similarly to those in text-based programming languages. They allow you to store and manipulate data...

 
 

Comentarios


Ya no es posible comentar esta entrada. Contacta al propietario del sitio para obtener más información.

Tel: 1-888-876-4265

© 2019 Rohbot Kit by illuminEd Inc

Sign up to receive Rohbot news and updates.

Thanks for submitting!

© Rohan  Jay  - High School Senior at  PRISMSUS 

american_citizenship_award_pin.jpg
image.png
image.png
bottom of page