Don't Blink LogoFTC Stack

Methods

Organize your code into reusable blocks.

Methods are reusable blocks of code that perform a specific task. Instead of writing the same code multiple times, you can write it once and call it whenever you need it.

Creating a Method

public void sayHello() {
    System.out.println("Hello!");
}

Call it like this:

sayHello();

Parameters

Methods can accept information called parameters.

public void drive(double power) {
    System.out.println(power);
}
drive(0.75);

Return Values

Some methods return a value.

public int square(int number) {
    return number * number;
}
int result = square(5);

If a method doesn't return anything, use void.

Scope

Variables only exist within the block where they are declared.

public void example() {
    int x = 5;
}

x cannot be used outside the method.

System.out.println(x); // Error

FTC Example

public void openClaw() {
    claw.setPosition(1.0);
}

public void closeClaw() {
    claw.setPosition(0.0);
}

Key Takeaways

  • Methods organize your code.
  • Parameters allow methods to receive data.
  • Methods can return values.
  • Local variables only exist within their scope.

Was this resource helpful?

On this page

Was this resource helpful?