Lesson 3.5: Methods: How to Write Them
Lesson 3.5: Methods: How to Write Them
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).
Practice Questions
int x = 10; new Adder().addFive(x); // addFive: n = n + 5 System.out.println(x);
public int getLarger(int a, int b) {
if (a > b) { return a; }
}
return b;.rocket.launch(100) where method does speed = speed * 2;, what is TRUE?public int computeTotal(int a, int b) { return a + b; }
public void resetScore() { score = 0; }
public String getLabel() { return 42; }
public boolean isActive() { return active; }
mystery(4) return?public int mystery(int n) { n = n * n; return n + 1; }
obj.swap(x,y) where x=3,y=7, swap correctly swaps a and b internally. What are x and y?add(int a, int b) and add(int x, int y)
int add(int,int) and double add(int,int)
int add(int a, int b) and int add(double a, double b)
void add(int a)
public int triple(int value) { value *= 3; return value; }
int n = 4;
int result = obj.triple(n);
Mastery: Methods
public int compute(int x, int y) {
if (x > y) { return x - y; }
else if (x < y) { return y - x; }
}
addToTotal does: amount += 100; total += amount;. val=50 passed. What is printed?System.out.println(val + " " + c.getTotal());
obj.increment(myCount) does count++;, myCount is unchanged. Which fix makes internal count increase?int process(int a) { return a*2; } and int process(int a, int b) { return a+b; }. What is process(3,4)?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]