UNIT 1

Method Signatures

TOPIC 1.9

AP Computer Science A

Understanding how methods work and how to call them

What is a Method?

Definition:

A method is a named block of code that only runs when it is called. A block of code is any section of code that is enclosed in braces.

Key Concepts:

  • Named block of code: Methods have specific names to identify them
  • Only runs when called: Methods don't execute automatically
  • Enclosed in braces: Method body is contained within { }
  • Procedural abstraction: You can use a method without knowing how it's written
public void printMessage() { System.out.println("Hello, World!"); }

Method Parameters

Parameter:

A variable declared in the header of a method or constructor that can be used inside the body of the method.

Purpose:

Parameters allow values (arguments) to be passed and used by a method or constructor.

public void greetUser(String name) { System.out.println("Hello, " + name + "!"); } public int addNumbers(int a, int b) { return a + b; }

In the examples above: name, a, and b are parameters.

Method Signatures

Method Signature:

For methods with parameters: method name + ordered list of parameter types

For methods without parameters: method name + empty parameter list

Examples:

// Method with parameters public int multiply(int x, int y) // Signature: multiply(int, int) // Method without parameters public void displayMenu() // Signature: displayMenu() // Method with different parameter types public void processData(String text, double value, boolean flag) // Signature: processData(String, double, boolean)

Void vs Non-Void Methods

Void Methods Non-Void Methods
Do not have a return value Return a value of a specific type
Not called as part of an expression Must store return value or use in expression
Used for actions/operations Used for calculations/data retrieval
// Void method example public void printScore(int score) { System.out.println("Score: " + score); } // Non-void method example public int calculateScore(int points, int bonus) { return points + bonus; }

How to Call Methods

Void Method Calls:

printScore(95); // Direct call greetUser("Alice"); // Pass argument

Non-Void Method Calls:

// Store return value in variable int totalScore = calculateScore(80, 15); // Use in an expression if (calculateScore(points, bonus) > 90) { System.out.println("Great job!"); } // Use in another method call System.out.println(calculateScore(75, 25));
Remember: Non-void methods must have their return value stored or used immediately.

Arguments vs Parameters

Argument:

A value that is passed into a method when the method is called.

// Method definition (has parameters) public void displayInfo(String name, int age) { System.out.println("Name: " + name + ", Age: " + age); } // Method call (passes arguments) displayInfo("John", 16); // "John" and 16 are arguments

Important Rules:

  • Arguments must be compatible in number and order with parameter types
  • Arguments are passed using call by value
  • Call by value initializes parameters with copies of the arguments

Call by Value

Call by Value:

When calling methods, arguments are passed using call by value. This initializes the parameters with copies of the arguments.

public void changeValue(int x) { x = 100; // This only changes the copy System.out.println("Inside method: " + x); // Prints 100 } public void testMethod() { int number = 50; changeValue(number); // Pass copy of number System.out.println("Original: " + number); // Still prints 50 }
Key Point: Changes to parameters inside the method do not affect the original arguments.

Method Overloading

Method Overloading:

Methods are said to be overloaded when there are multiple methods with the same name but different signatures.

// All these methods are overloaded versions of "print" public void print(String message) { System.out.println("String: " + message); } public void print(int number) { System.out.println("Integer: " + number); } public void print(String message, int count) { for (int i = 0; i < count; i++) { System.out.println(message); } }

Each method has a different signature based on its parameter list.

Method Call Flow of Control

A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing.

public void mainProgram() { System.out.println("Step 1"); // Executes first helperMethod(); // Jumps to helper method System.out.println("Step 3"); // Executes after helper returns } public void helperMethod() { System.out.println("Step 2"); // Executes second // Control returns to calling method }

Flow: Step 1 -> Step 2 -> Step 3

Once the last statement in the method is executed or a return statement is executed, control returns to the point immediately following where the method was called.

Common Method Examples

// Constructor-like method public void initializeGame(String playerName, int level) { this.playerName = playerName; this.currentLevel = level; this.score = 0; } // Calculation method public double calculateGPA(int totalPoints, int creditHours) { return (double) totalPoints / creditHours; } // Boolean method public boolean isPassingGrade(double grade) { return grade >= 60.0; } // Void method with multiple parameters public void displayStudentInfo(String name, double gpa, boolean passing) { System.out.println("Student: " + name); System.out.println("GPA: " + gpa); System.out.println("Passing: " + passing); }

Key Takeaways

Remember These Concepts:

  • Method Signature: Name + parameter types (order matters!)
  • Void methods: Perform actions, no return value
  • Non-void methods: Return values that must be used
  • Parameters vs Arguments: Parameters in definition, arguments in calls
  • Call by Value: Methods work with copies of arguments
  • Method Overloading: Same name, different signatures
  • Flow of Control: Execution jumps to method, then returns
Practice: Identify method signatures, distinguish void from non-void methods, and trace method call execution!
1 / ?