Reference

Video Lessons

Learning Objectives

Subprocedures

In Procedures, we mentioned four ways that a procedure can be used:

  • in the Command Center
  • in the Rules tab of a turtle's backpack
  • in a button
  • in the definition of another procedure
This includes procedures you have written yourself as well as primitive procedures. In this section, we will talk more about using a procedure you already defined inside the definition of another procedure you are writing.

Suppose that you have defined the square procedure:

to square
fd 50 rt 90
fd 50 rt 90
fd 50 rt 90
fd 50 rt 90
end

and the triangle procedure:

to triangle
fd 50 rt 120
fd 50 rt 120
fd 50 rt 120
end

(The text below is from MicroWorlds EX > Programming > Programming Fundamentals > Writing Procedures)

You can now use these procedures to define the house procedure:

to house
square
forward 100
right 30
triangle
end

When you run house, you are running the procedures square and triangle indirectly. House runs them for you. Here is a model of what is happening:

MicroWorlds goes through each instruction in the house procedure. When it sees the instruction for , it looks for the definition of square and runs the body of that procedure. In the case of square, the body is fd 50 rt 90 fd 50 rt 90 fd 50 rt 90 fd 50 rt 90.
Then it goes on to forward and right (MicroWorlds knows that these are primitives).
When it reaches triangle, it looks for the definition of triangle and runs that procedure.
Then MicroWorlds goes on to the next line of house which is end. Now house is finished.

Square and triangle are subprocedures of house. House is the superprocedure of square and triangle.

About the Center for Talent Development

Center for Talent Development (CTD), housed at Northwestern University's School of Education and Social Policy, is an accredited learning center and research facility that identifies, educates and supports gifted students and their families and serves as a leader in gifted education. Learn more about the Center for Talent Development.