πŸ““2.4: Methods with Parameters

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 2.4

πŸ“ Take notes in a Codespace during class, coding along with the instructor.

  1. Go to GitHub and click on your picture in the TOP RIGHT corner
  2. Select Your repositories
  3. Open CS2-Unit-2-Notes
  4. Now on your repository, click and select the Codespaces tab
  5. Click Create Codespace on main (unless you already have one listed there), wait for the environment to load, then you’re ready to code!

Calling Methods With Parameters

In the last lessons, we used simple methods like forward and turnRight to make the turtle draw lines. You may have noticed that forward() and backward() always move the same number of pixels (100 pixels), and turnRight() and turnLeft() always turn at right angles (90 degrees). This is a little limiting. What if we wanted to draw a triangle or the letter A? These require smaller angles to draw diagonal lines and different length lines. Luckily, there are more complex methods in the Turtle class that let you specify the number of pixels to move forward or the number of degrees to turn. These values that you can give to methods to help them do their job are called arguments or parameters.

The parentheses () after method names when we call a method are there in case you need to give the method actual parameters or arguments (some data) to do its job. For example, we can give the argument 100 in forward(100) to make the turtle go forward 100 pixels or the argument 30 in turn(30) to make the turtle turn 30 degrees instead of 90 degrees.

object.method(arguments); is used to call an object’s method and provide some arguments (actual parameters) to do its job.

Parameters vs. Arguments

Although some people use the words parameters and arguments interchangeably, there is a subtle difference:

  • When you define your own method, the variables you define for it are called formal parameters.
  • When you call the method to do its job, you give or pass in arguments or actual parameters to it that are then saved in the parameter variables.

So, in the definition of the forward method, it has a parameter variable called pixels, and in the call to forward(100), the argument is the value 100 which will get saved in the parameter variable pixels. You will learn to write your own methods in Unit 5. In this unit, you will learn to call methods that are already written for you.

// Method call
yertle.forward(100); // ARGUMENT is 100

// Method definition written for you in the Turtle class
public void forward(int pixels) // PARAMETER is pixels

Tracing Methods

You will not write your own methods until Unit 5, but you should be able to trace and interpret method calls.

Check out another version of the Old MacDonald Song with a more powerful abstraction. The method verse now takes 2 parameters for the animal and the noise it makes, so that it can be used for any animal. Use the Java Visualizer to step through the code.

πŸ’» In-Class Activity

  1. Go to
  2. Make sure you SIGN IN!
  3. Complete the Programming Challenge: Turtle House activity in pairs.

⭐️ Summary

  • Methods define the behaviors or functions for objects.

  • To use an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example object.method();

  • Some methods take parameters/arguments that are placed inside the parentheses object.method(arguments).

  • Values provided in the parameter list need to correspond to the order and type in the method signature.


πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK!

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Click the button on the LEFT menu
  3. Type a brief commit message at the top of the file that opens, for example: updated Main.java
  4. Click the small βœ”οΈ checkmark in the TOP RIGHT corner
  5. Click the button on the LEFT menu
  6. Finally you can close your Codespace!

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.