3.3 Methods with Parameters

Writing Java Methods with Parameters

Unit 3: Methods, Worlds, and Constructors · Lesson 3.3 · about 30 minutes · no coding experience needed

A method that always does exactly the same thing is useful. A method you can aim is far more useful, and the difference is one word inside the parentheses.

What you will be able to do

  • Write a method that takes a parameter
  • Call it with different arguments and predict the result
  • Explain why the parameter name is invisible outside the method

Words you will need

Parameter
In plain words The slot in the definition.
More precisely A typed, named variable declared in the method signature.
Argument
In plain words The actual value the caller supplies.
More precisely The expression passed at the call site for a parameter.

Build it step by step

Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.

  1. Step 1

    On your screen A method with a single int parameter, with the parameter highlighted in the signature and again in the body.

    public void addScore(int points)
    {
        score = score + points;
    }

    `int points` inside the parentheses is the parameter. It declares a variable that only exists inside this method, and whoever calls decides what goes in it.

    Inside the body you use it like any other variable.

  2. Step 2

    On your screen Three different calls to the same method with different values.

    addScore(1);    // a coin
    addScore(10);   // a gem
    addScore(50);   // the treasure chest

    One method, three behaviors. That is the whole point.

    Without the parameter you would need addOne(), addTen() and addFifty(), and a fourth method the moment you invent a new pickup.

  3. Step 3

    On your screen A call passing a variable whose name differs from the parameter name.

    int gemValue = 10;
    addScore(gemValue);   // points becomes 10 inside the method

    The names do not have to match, and this is worth pausing on.

    `gemValue` is the caller's name for it. `points` is the method's name for it. The VALUE is copied across; the names are private to each side.

    Inside addScore there is no such thing as gemValue. Outside it, there is no such thing as points.

  4. Step 4

    On your screen A method taking two parameters separated by a comma.

    public void moveBy(int dx, int dy)
    {
        setLocation(getX() + dx, getY() + dy);
    }
    
    moveBy(3, 0);    // right
    moveBy(0, -3);   // up

    Two parameters, separated by a comma. The caller must supply two arguments, in the same order.

    Order is positional, not by name: `moveBy(0, -3)` puts 0 into dx and -3 into dy. Swap them and the actor goes the wrong way, with no error, because both are ints.

Mistakes almost everyone makes here

These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.

Common wrong idea The variable you pass in must have the same name as the parameter.

What is actually true The names are unrelated. Only the value crosses over.

Why it matters This belief makes students rename variables constantly to satisfy a rule that does not exist.

Common wrong idea The parameter name can be used outside the method too.

What is actually true It exists only inside. Using it outside is cannot find symbol.

Why it matters Understanding this is the beginning of understanding scope.

Common wrong idea Arguments are matched by name.

What is actually true They are matched by POSITION, in order.

Why it matters Two parameters of the same type in the wrong order produce a silent bug.

Common wrong idea A method can be called with fewer arguments than it declares.

What is actually true You must supply exactly as many as the signature lists.

Why it matters Otherwise Java reports "cannot be applied to given types", which does not obviously mean "you missed one".

Check your understanding

Answer these before moving on. They are graded instantly and you can retry.

  1. 1. In `public void addScore(int points)`, what is `points`?

  2. 2. int gems = 7; addScore(gems); What is the value of `points` inside the method?

  3. 3. Can you use the parameter name `points` in act(), outside addScore?

  4. 4. `moveBy(int dx, int dy)` is called as `moveBy(0, 5)`. What is dy?

  5. 5. Why write `addScore(int points)` instead of separate addOne and addTen methods?

Fill in the code

Write a method that makes the actor jump upward by a chosen amount. Remember which direction y grows in.

public void jump( height)
{
    setLocation(getX(), getY()  );
}
Stuck? Open a hint for each blank
  • Blank 1: A whole number of cells.
  • Blank 2: Up the screen is a SMALLER y.
  • Blank 3: The name of the slot declared in the parentheses above.

Lesson quiz

This one counts toward your progress. Take it when the section above makes sense.

  1. 1. A parameter is visible:

  2. 2. How are arguments matched to parameters?

  3. 3. `public void grow(int amount)` is called as `grow();`. What happens?

  4. 4. What is the advantage of a parameter over a fixed value in the method body?

The short version

  • A parameter is a slot in the definition; an argument is the value supplied.
  • The names on each side are unrelated. Only the value crosses.
  • A parameter exists only inside its own method.
  • Arguments are matched by POSITION, not by name.

If you get stuck

These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]