Unit 3, Chapter 1, Lesson 8: Creating Functions with Parameters
In this lesson, students practice using and creating functions with parameters. Students learn that writing functions with parameters can generalize solutions to problems even further. Especially in situations where feel like you are about to duplicate some code with only a few changes to some numbers, that is a good time to write a function that accepts parameters. In the second half of the lesson, students make a series of modifications to a program that creates an “Under the Sea” scene by adding parameters to functions to more easily add variation to the scene. Lastly, students are introduced to App Lab’s random number functions to supply random values to function calls so the scene looks a little different every time the program runs.
Writing functions with parameters is a simple idea, but it traditionally has some devilish details for new learners of programming. The basic idea is that you often want to write a function that takes some input and performs some action based on that input. For example, the turtle function moveForward is much more useful when you can specify how much to move forward (e.g., moveForward(100)), rather than just a fixed amount every time. It’s very common to encounter situations where as a programmer you realize that you basically need a duplicate of some code you’ve already got, but you just want to change some numbers. That’s a good time to write a function with a parameter; the parameter just acts as a placeholder for some value that you plug in at the time you call the function. Just like it’s considered good practice to give descriptive names to your functions, the same is true for the names of the parameters themselves. For example:
drawSquare(sideLength) is better than
Students will be able to:
- Write functions with parameters to generalize a solution instead of duplicating code.
- Identify appropriate situations for creating a function with parameters.
- Use random numbers as inputs to function calls for the purpose of testing.
- Add parameters to a function in an existing piece of code to generalize its behavior.
- Parameter – An extra piece of information passed to a function to customize it for a specific need
- In the previous lesson, we learned to use a lot of new turtle commands. Some of these commands accept a parameter, or even many parameters, which allow us to pass values to the function. This allowed us to make much more interesting images by specifying precisely how far the turtle should move or turn, and introduced the ability to choose specific pen sizes and colors.
- 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 functions, one for each number of degrees we wanted to turn!
- Parameters allow us to use a single function as a general solution to a whole group of problems.
- This is clearly a useful construct to use in our programs, and in today’s lesson we’re going to learn how to create functions with parameters for ourselves.
- The levels in Code Studio for this lesson walk students through a number of small exercises that build up to making a small “under the sea” drawing.
- Log in to code.org
- Work through the lesson on your own–be sure to READ EVERYTHING!!
- Evaluate at least 1 program someone else did. Include the name of the person whose code you critiqued. Use the following criteria:
- The program draws the diamond
- The program defines four functions: right(); drawStep(); drawSide(); and drawDiamond(); The names are less important than the existence of four functions with this functionality.
- The program makes a single call to drawDiamond();
- The program looks clean and organized
- When do you need a function with a parameter?
- Having used parameters to generalize the behavior of many functions, students should attempt to formalize their understanding of what parameters are and why they are beneficial. Important points:
- Parameters allow the creation of a generalized solution to a whole class of problems, rather than individually solving many specific problems.
- Parameters remove the need to create repetitive functions, making code easier to write, read, and change.
- In your Word Online file for this class, develop a rule for deciding when to create a function with a parameter rather than a normal function. Below your rule write a couple sentences justifying your rule. Share your rules with the class.
- Every time you added a parameter to a function, you generalized the behavior of the function without having to rewrite the code.
- In your Word Online file for this class, respond to the following:
- Multiple Choice: Which of the following are true?
- Functions with parameters can be used to prevent the creation of duplicated code.
- Parameters can only be used once within the body of a function.
- Parameters generalize the solution of a specific problem.
- Parameters need not be provided to a function in any particular order.
- 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 functions with parameters an example of abstraction?
- Multiple Choice: Which of the following are true?