In this lesson, the students will practice using and creating actions with parameters. They will learn that writing actions with parameters can generalize solutions to problems even further. Especially in situations where feel like the students are about to duplicate some code with only a few changes to some numbers, it is a good idea to write an action that accepts parameters. In the later part of the lesson, the students will be introduced to one of the Quorum Programming Language's actions that will generate random numbers. Through this lesson, the students will be previewing a portion of a much larger picture of computer programming by being introduced to some of the key concepts of many modern programming languages - commonly known as "Object Oriented Languages."


Students will be able to:


Writing actions with parameters is a simple idea, but it traditionally has some tricky details for new learners of programming. The basic idea is that programmers often want to write an action that takes some input and performs some action based on that input. For example, the turtle action MoveForward is much more useful when the programmer can specify how much to move forward (e.g., RepeatMoveForward(6)) rather than just a fixed amount every time. It's very common to encounter situations where, as a programmer, the student might realize that he or she basically needs a duplication of some code that he or she already has, but in which they just want to change some numbers. This is a good time to write an action with a parameter; the parameter just acts as a placeholder for some value that a programmer plugs in at the time the action is called. Just like it's considered good practice to give descriptive names to the actions, the same is true for the names of the parameters themselves. For example: DrawSquare(sideLength) is better than DrawSquare(stuff).

Getting Started

New Code

In the previous lesson, the students have learned to use a lot of new turtle commands. Some of these commands accept a parameter, or even many parameters, which allow the students to pass values to the action. Parameters are a powerful programming construct. Suppose we have a whole group of similar problems, like turning the turtle some amount. Without a parameter we would need 360 different actions, one for each number of degrees we wanted to turn! Parameters allow students to use a single action as a general solution to a whole group of problems. This is clearly a useful construct to use in their programs, and in today's lesson the students are going to learn how to create actions with parameters for themselves.

Activity: Coding Challenge

Pre-Activity: Video "Introduction to Functions"

Teacher's Tip

In the following video, the concept of a "Function" is described with the example of the "Turtle Programming tool" used in the Code.org curriculum. While the "Turtle Programming tool" primarily uses the "block language" (a language that could be written by combining picture blocks on the computer screen), the "Quorum Turtle Module" uses "text language" as the students have already experienced in the previous lessons. Although the style of the coding is different between the two languages, the concept that supports the execution of the code/command is very similar. Therefore, watching this video before the students start the activity is still beneficial for their learning.

Challenge 16: Defining and Using an Action with a Parameter

From the video, the students have learned that defining multiple actions to draw different sized squares results in lots of repetitive actions - they are essentially the same, with a few numbers changed - and this is not desirable. Remind students that any time they find themselves effectively copy and pasting large sections of code, it is usually a sign that there is a better way.

Student Instructions

For this challenge, you are going to create your own action that accepts parameters. Create an action that draws a square with different sizes determined by your parameters.

Small Big Squares

Your final answer should be a modified version of the template below. Note that it will not match the image above, at least not initially.

Quorum Turtle Module: Challenge 16

The drawing shown in the picture above is not what we created in our code. This is because we have given students the DrawSquare(size) action that was created in the video, but it's not quite right. Ask the students to fix the code inside the TurtleCommands action so that the program creates the drawing correctly. This will give them to opportunity to call the DrawSquare(size) action twice, with different values each time.

If the students are successful, they will be able to see the following lines: (0, 0) to (0, 1) to (1, 1) to (0, 1) to (0, 0) for the first square, and (2, 2) to (5, 2) to (5, 5) to (5, 2) to (2, 2) for the second square.

Analyzing the code above

The following is the sequence how the computer executes/runs this code, using the template's call of DrawSquare(2) as an example.

  1. The computer places the turtle at position (0, 0) because of SetStartingCoordinates(0, 0).
  2. The computer finds where we call the action DrawSquare(2), and then immediately looks elsewhere to find the place this code is defined.
  3. When the computer finds the action DrawSquare(integer size), it automatically recognizes the word "size" is not text. Rather, it is the name for an integer variable because it is defined as an "integer" before it says "size."
  4. Then computer remembers that the programmer placed the integer "2" when he or she called this exact action in the middle part of the program.
  5. Therefore, the computer replaces the word "size" with the integer "2" when it runs the code inside of the action DrawSquare(size).
  6. This results in the turtle drawing a straight line with a length of two blocks every time it runs MoveForward(size) action inside of the action DrawSquare(size).
  7. When the DrawSquare(size) action is finished running, we will have a square and the computer returns to the line after we called DrawSquare(2) in the TurtleCommands action.

Note: In Quorum language, the students need to specify the type of parameter that the action accepts. There are four primitive types in Quorum Language - number, integer, text and boolean. Throughout this lesson, we will only have students use the "integer" type. As such, they nned to make sure the students write this word before they place the name of the parameter when they are defining their own action.

Challenge 17: Defining Actions with Multiple Parameters

Now it's the student's turn to define a function that accepts multiple parameters. The concept used is exactly the same as in the previous Challenge. The only difference is that their action should now accept two parameters. If the students would like, they can modify their code from the previous Challenge to complete this Challenge.

In this challenge, the students will create and call an action that produces a rectangle with their choice of width and height. When they create an action that accepts multiple parameters, they need to separate the parameters by comma both when they are defining the action and when they are calling the action. For example, they would write the action MyAction(integer width, integer height) when they define the action. They might write MyAction(5, 7) when they call the acton.

If they have successfully completed the previous challenges, students should have all the knowledge and skills they need to complete this challenge. Just remind them of the following: they must separate their parameters with commas and should give them descriptive and meaningful names.

Introducing the Random Input

Programmers often use the random number generator to make some part of their program "unpredictable" - even for the programmers themselves. Many programming languages have their own random number generators. Commonly, programmers set up a random number generator with a range of numbers by setting maximum number and minimum number. For example, if the minimum number is set to 1 and maximum to 10, the random number generator will only generate a random number between 1 and 10 (1, 2, 3, 4, 5, 6, 7, 8, 9, or 10).

In the Quorum Programming Language, setting up the "Random Number Generator" requires the students to learn some more basics of computer programming. They will learn the basics in detail later, in Unit 5. So for this part of the lesson, we have provided a template below to explain how it needs to be done. This also serves as a great preview to concept of an "Object Oriented Language," which is what the Quorum Programming Language is.

Student Instructions

For this challenge, a template has been provided that generates random integers for you. Read through the comments in the template code to try to understand how to generate a random integer.

Quorum Turtle Module: Challenge 17

Note that since the computer remembers this randomly generated number in the "randomInteger1" variable, we can use this variable and the value inside the code that we will be writing below those lines.

Now, try to write an action named DrawRectangle(integer width, integer height) that draws a rectangle. Then, call this action in the turtle commands using the random integers that you generated, DrawRectangle(randomInteger1, randomInteger2).

Wrap Up

Discussion: When do you need an action with a parameter?

Ask the students to develop a rule for deciding when to create an action with a parameter rather than a normal action. Below their rule, they should write a couple of sentences justifying their rule. Some examples include:

Discussion Goal:

Having used parameters to generalize the behavior of many actions, try to formalize the students' understanding of what parameters are and why they are beneficial. Important points to draw out are: (1) Parameters allow the creation of a generalized solution to a whole class of problems, rather than individually solving many specific problems, (2) Parameters remove the need to create repetitive actions, making the code easier to write, read, and change.


Multiple Choice: Which of the following are true?

Free Response:

"Abstraction" is often used to indicate cases where we focus on a general case and ignore a specific instance of a problem. Given this meaning of the word, how are actions with parameters an example of abstraction?

Extended Learning

The students can try to create actions with parameters to draw different sizes of a given shape.

Standards Alignment