Skip to main content
United StatesComputer ScienceSyllabus dot point

How do you pass arguments to a void method, and how do parameters and arguments correspond?

Topic 2.4 Calling a Void Method with Parameters: call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and explain how arguments are copied to parameters.

A focused answer to AP CSA Topic 2.4, covering parameters versus arguments, matching the argument list in number, order and type, pass-by-value for primitives, overloaded methods, and how the call supplies data to the method, with a worked trace.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Parameters versus arguments
  3. Matching number, order and type
  4. Pass by value
  5. Overloaded methods
  6. Try this

What this topic is asking

The College Board (Topic 2.4) wants you to call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and to understand that for primitives the argument's value is copied into the parameter (pass by value). This is how a method receives the data it needs to do its job.

Parameters versus arguments

If a class defines void deposit(double amount), then amount is the parameter. When you call account.deposit(250.0), the value 250.0 is the argument, and it is copied into amount for that call. The names need not match; only the types and order must line up.

Matching number, order and type

So for void printLabel(String name, int copies):

  • printLabel("Tom", 3) matches (String, then int).
  • printLabel(3, "Tom") does not - the order is wrong.
  • printLabel("Tom") does not - too few arguments.

Pass by value

Java passes arguments by value: the value of the argument is copied into the parameter. For a primitive argument, the method works on its own copy, so changing the parameter inside the method does not change the caller's original variable. (Objects passed as arguments copy the reference, so the method can change the object's state through it, but that is a finer point; for primitives, the caller's variable is safe.)

Overloaded methods

A class can have several methods with the same name but different parameter lists - this is overloading. The compiler selects the version whose parameters match the arguments you pass. For example, print(int n) and print(String s) are two different methods; obj.print(5) calls the first, obj.print("hi") the second.

Try this

Q1. A method header is void setSpeed(int speed). Write a valid call on an object car that sets the speed to 60. [1 point]

  • Cue. car.setSpeed(60);

Q2. Explain why a void method changing its int parameter does not change the caller's matching variable. [2 points]

  • Cue. Java passes primitives by value: the argument's value is copied into the parameter, so the method works on a copy and the caller's original variable is untouched.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. A class `Printer` has a void method `printLabel(String name, int copies)`. Which call is valid? (A) `p.printLabel(3, "Tom");` (B) `p.printLabel("Tom");` (C) `p.printLabel("Tom", 3);` (D) `p.printLabel("Tom", 3.0);` (E) `printLabel("Tom", 3);`
Show worked answer →

The answer is (C).

Arguments must match the parameter list in number, order and type. printLabel(String name, int copies) expects a String then an int, so p.printLabel("Tom", 3) matches. (A) has the arguments in the wrong order (an int then a String). (B) passes only one argument. (D) passes a double where an int is required, which does not match. (E) omits the object reference, so the non-static method has no object to act on.

Markers reward matching number, order and type of arguments, and using dot notation on an object.

AP 2020 (style)4 marksFree response (code writing). A class `Account` has a void method `deposit(double amount)` and a void method `transfer(Account other, double amount)`. Write a code segment that creates two `Account` objects `a` and `b` (using a no-argument constructor), deposits 250.0 into `a`, then transfers 100.0 from `a` to `b`.
Show worked answer →

A 4-point question testing void calls with parameters, including an object parameter.

Account a = new Account();
Account b = new Account();
a.deposit(250.0);
a.transfer(b, 100.0);

Point 1: creating both objects. Point 2: a.deposit(250.0) passes a double argument matching the parameter. Point 3: a.transfer(b, 100.0) passes the other account and the amount in the correct order. Point 4: calling on the correct object (a, the source) with arguments matching the parameter list. Swapping the order in transfer, or passing the wrong types, loses marks.

Related dot points

Sources & how we know this