1.5 Parameters and Return Values
Parameters and Return Values in Java
Unit 1: Meet Greenfoot · Lesson 1.5 · about 30 minutes · no coding experience needed
Every method you will ever meet answers two questions: what do I have to give it, and what does it give me back? Learn to read those two answers off a single line and you can use a method you have never seen before.
What you will be able to do
- Read a method signature and say what it needs and what it returns
- Explain what void means
- Use a returned value inside another method call
Words you will need
- Parameter
- In plain words The slot. What the method needs you to supply.
- More precisely A named, typed variable in a method definition, filled in by the caller.
- Argument
- In plain words The actual value you put in the slot.
- More precisely The expression supplied at the call site for a given parameter.
- Return type
- In plain words What kind of answer you get back, or void for none.
- More precisely The type written before the method name, describing the value the method produces.
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.
-
Step 1
On your screen Greenfoot documentation for the Actor class showing the signature of the move method.
void move(int distance)Read it left to right and it answers both questions.
`void` is the return type: this method hands nothing back. `move` is the name. `(int distance)` is the parameter list: you must supply one whole number, and inside the method it will be called distance.
So a legal call is move(4). Not move(). Not move("far"). One whole number.
-
Step 2
On your screen Documentation showing the getX method signature with an empty parameter list and an int return type.
int getX()This one is the mirror image. `int` means it hands back a whole number. `()` empty means it needs nothing from you.
That difference changes how you use it. move(4) is a complete instruction on its own. getX() is a question, and a question you never listen to the answer of is pointless.
-
Step 3
On your screen A crab in the world with its current x coordinate displayed in a Greenfoot result dialog.
int where = getX();One way to listen: store the answer. `getX()` runs and produces a number; `int where =` catches it and gives it a name. Now you can use `where` as many times as you like.
-
Step 4
On your screen The crab shifted one cell to the right after a setLocation call built from getX and getY.
setLocation(getX() + 1, getY());The other way: use the answer immediately, inside another call. Read this from the inside out. getX() gives the current x. Add 1. getY() gives the current y, unchanged. Hand both to setLocation.
The result is "move one cell right, whichever way I happen to be facing". You will write this exact line in Unit 2 to make arrow keys work, so it is worth reading twice.
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 void means the method is empty or broken.
What is actually true void means it does something useful but hands no value back.
Why it matters move is void and is the most useful method in Unit 1.
Common wrong idea getX() moves the actor to x.
What is actually true getX() only reports. It changes nothing.
Why it matters Methods that ask and methods that act are different tools; mixing them up produces code that seems to do nothing.
Common wrong idea You can leave the parentheses off getX because it takes nothing.
What is actually true Empty parentheses are still required: getX().
Why it matters This is a compiler error beginners hit constantly, and the message is not obvious.
Common wrong idea setLocation(getX() + 1, getY()) needs the actor to face right.
What is actually true It ignores facing entirely. It adds one to the x coordinate.
Why it matters Understanding this is what separates coordinate movement from move/turn movement.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. A method signature reads `void turn(int amount)`. What does it give back?
-
2. Given `int getY()`, which call is written correctly?
-
3. An actor is at (7, 3). What does `setLocation(getX() + 2, getY());` do?
-
4. In `move(6)`, what is 6 called?
-
5. Why is `int where = move(4);` wrong?
Fill in the code
This line should move an actor one cell DOWN, leaving its x coordinate alone. Remember which direction y grows in Greenfoot.
setLocation(, + );
Stuck? Open a hint for each blank
- Blank 1: x is not changing, so ask the actor for its current x.
- Blank 2: Start from the current y.
- Blank 3: y grows downward, so moving down means adding this much.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. What does the `int` at the start of `int getX()` tell you?
-
2. Which of these is a valid call to `void move(int distance)`?
-
3. An actor is at (2, 8). After `setLocation(getX(), getY() - 1);` where is it?
-
4. What is the difference between a parameter and an argument?
The short version
- A signature answers two questions: what must I give it, and what do I get back?
- void means nothing comes back. It does not mean nothing happens.
- Empty parentheses are still required: getX(), never getX.
- A returned value can be stored in a variable or used directly inside another call.
- setLocation(getX() + 1, getY()) moves one cell right regardless of facing.
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]