Lesson 3.5: Methods: How to Write Them

Unit 3 · Lesson 3.5 · Code Mechanics

Lesson 3.5: Methods: How to Write Them

🕑 35–40 min· 8 Practice Questions· 4 Mastery Questions· Output Predictor + Bug Hunt

What You'll Learn

  • Write void and return methods with correct headers and return statements
  • Explain pass-by-value for primitive parameters
  • Identify missing return statements that cause compile errors
  • Distinguish local variables from instance variables
  • Recognize valid method overloading

Key Vocabulary

Term Definition
void method Performs an action but returns NO value (EK 3.5.A.1)
return method Computes and returns a value; return type in header (EK 3.5.A.2)
parameter Variable in method header that receives a passed argument (EK 3.5.A.7)
argument Actual value passed into a method when called
pass by value Primitive argument passes a COPY; changes to parameter do NOT affect original (EK 3.5.A.8)
method signature Method name + ordered parameter types; used to distinguish overloaded methods
local variable Variable declared inside a method; exists only during that method's execution

CED EK 3.5.A.1–3.5.A.8

A void method performs an action without returning a value (EK 3.5.A.1). A non-void method returns a value matching its declared return type (EK 3.5.A.2). When a primitive argument is passed, the parameter receives a COPY -- the original is unaffected (EK 3.5.A.8).

Method Anatomy

//  access   return   name       parameter list
    public   double   getArea(int length, int width) {
        return (double)(length * width);
    }

void Methods vs Return Methods

void vs return

// void: performs action, returns nothing
public void printInfo() {
    System.out.println("Name: " + name);
}

// return method: computes and returns a value
public int getDoubled(int n) {
    return n * 2;
}
int result = obj.getDoubled(5);  // result = 10

Pass by Value: Primitives Are Copied

Pass by Value Demo

public class Modifier {
    public void addTen(int x) { x = x + 10; }
}

int num = 5;
new Modifier().addTen(num);
System.out.println(num);  // prints 5, NOT 15

The parameter x receives a COPY of 5. Changing x inside the method does nothing to num. This is EK 3.5.A.8 and one of the most tested AP exam concepts.

AP Trap: Missing Return Statement

public int getMax(int a, int b) {
    if (a > b) { return a; }
    // BUG: no return when a <= b
}

Every code path in a non-void method must return a value. This is a compile error.

AP Trap: Return Type Mismatch

public int getLabel() {
    return "Hello";  // BUG: returns String, declared int
}

The return value must match the declared return type. Mismatch = compile error.

AP Trap: Pass by Value -- Primitives Never Change

public void doubleIt(int x) { x *= 2; }

int score = 10;
obj.doubleIt(score);
// score is still 10

Students expect score to become 20. It doesn't. x was a copy (EK 3.5.A.8).

Real-World Connection: The Math class consists entirely of static return methods: Math.sqrt(), Math.abs(). Each takes parameters and returns a computed value.

Summary

  • A void method performs an action; a return method returns a value matching its declared type.
  • Every code path in a non-void method must end with a return of the correct type.
  • When a primitive is passed as an argument, the parameter receives a COPY (EK 3.5.A.8); the original is unaffected.
  • A local variable declared inside a method exists only for that call.
  • Method signature = name + parameter types (used for overloading).
Tier 2 · AP Practice

Practice Questions

MCQ 1
What is printed?
int x = 10;
new Adder().addFive(x);
// addFive: n = n + 5
System.out.println(x);
⚠ Predict the answer before reading the options.
A 15
B 10
C 5
D Compile error
B is correct. Primitive passes by value. n is a copy; x stays 10.
MCQ 2
Will NOT compile. Why?
public int getLarger(int a, int b) {
    if (a > b) { return a; }
}
A Should be void
B Accessors can't take two parameters
C No return when a <= b -- compile error
D Single-char names not allowed
C is correct. Missing return when a <= b. Fix: add return b;.
MCQ 3
After rocket.launch(100) where method does speed = speed * 2;, what is TRUE?
A Instance variable speed is 200
B Parameter speed is 200 inside method; caller variable unchanged
C Compile error
D Caller variable is now 200
B is correct. Parameter receives a copy. Caller variable unaffected (EK 3.5.A.8).
MCQ 4
Which method header causes a compile error?
⚠ Predict the answer before reading the options.
A public int computeTotal(int a, int b) { return a + b; }
B public void resetScore() { score = 0; }
C public String getLabel() { return 42; }
D public boolean isActive() { return active; }
C is correct. Declared String but returns int -- type mismatch.
MCQ 5
What does mystery(4) return?
public int mystery(int n) { n = n * n; return n + 1; }
A 5
B 9
C 16
D 17
D is correct. 4*4=16, 16+1=17.
MCQ 6
After obj.swap(x,y) where x=3,y=7, swap correctly swaps a and b internally. What are x and y?
⚠ Predict the answer before reading the options.
A x=7, y=3
B x=3, y=7
C x=3, y=3
D x=7, y=7
B is correct. Swaps local copies only. Caller x and y unaffected.
MCQ 7
Which pair is valid method overloading?
A add(int a, int b) and add(int x, int y)
B int add(int,int) and double add(int,int)
C int add(int a, int b) and int add(double a, double b)
D Two identical void add(int a)
C is correct. Different parameter types = valid overload. A: same types. B: return type only. D: duplicate.
MCQ 8
What are n and result?
public int triple(int value) { value *= 3; return value; }
int n = 4;
int result = obj.triple(n);
A n=4, result=12
B n=12, result=12
C n=4, result=4
D n=12, result=4
A is correct. n stays 4. value (copy) becomes 12. result=12.
PRACTICE WITH A GAME — CHOOSE ONE:
Output Predictor: Method Calls
Predict carefully -- pass-by-value matters.
Question 1 of 3  ·  Score: 0

Done!
You got 0 of 3 correct.
Bug Hunt: Methods
Click the buggy line, or 'No bug'.
Question 1 of 3  ·  Score: 0
No bug — code is correct
Done!
You got 0 of 3 correct.
Tier 3 · AP Mastery

Mastery: Methods

MCQ 1
Will NOT compile. Why?
public int compute(int x, int y) {
    if (x > y) { return x - y; }
    else if (x < y) { return y - x; }
}
A Returns 0 when x==y is invalid
B Non-void can only have one return statement
C When x==y, no code path returns a value
D Compiles but throws exception at runtime
C is correct. When x==y neither branch runs. Compile error.
MCQ 2
addToTotal does: amount += 100; total += amount;. val=50 passed. What is printed?
System.out.println(val + " " + c.getTotal());
A 150 50
B 50 150
C 50 50
D 150 150
B is correct. val stays 50 (pass by value). amount=150 inside method. total=150. Output: 50 150.
MCQ 3
After obj.increment(myCount) does count++;, myCount is unchanged. Which fix makes internal count increase?
A Change parameter type to String
B Return int and return count
C Rename parameter to x
D Remove parameter; modify instance variable directly
D is correct. Pass by value means the only way to change object state is to modify the instance variable directly.
MCQ 4
int process(int a) { return a*2; } and int process(int a, int b) { return a+b; }. What is process(3,4)?
A 6
B 14
C 7
D Compile error
C is correct. Two arguments: two-parameter overload selected. 3+4=7.

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]