Methods#
Reusable blocks of code
Overview#
A method is a named block of code that performs a specific task. In more simple terms, a method is a function inside a class. Methods let you write logic once and call (use) it as many times as needed. This is better than copying and pasting: if you copy the same logic into five places and later find a bug, you have to fix it five times — and if you miss one, the bug remains. With a method, you fix the bug once in the method definition, and every call to that method automatically gets the fix.
See table of contents for a breakdown of this section.
Declaring a Method#
Note
In FRC examples, motor is a variable that holds a motor controller object (such as a SparkMax or TalonFX — small electronic devices that control how fast a motor spins). Calling motor.set(speed) or motor.setControl(...) is itself a method call into the vendor library. You will see this pattern throughout FRC code.
Every method declaration has four parts:
| Part | Example | Meaning |
|---|---|---|
| Access modifier | public |
Who can call this method |
| Return type | void |
What value this method gives back (void = nothing) |
| Name | setSpeed |
How you call it |
| Parameters | (double speed) |
Inputs the method needs to do its work |
Return Types#
A method either returns a value or returns nothing.
void— the method does something but gives nothing back (returns nothing) to the code that called it- Any data type — the method computes and returns a value of that type
// Returns nothing — just sets the motor
public void doSomething(parameter_type parameterName) {
// Do things here.
}
// Returns a boolean — whether the encoder has reached the target
public boolean atTarget() {
return encoder.getPosition() >= targetPosition;
}
// Returns a double — the current motor output
public double getSpeed() {
return motor.get();
}
// Returns nothing — just sets the motor
public void doSomething(parameter_type parameterName) {
// Do things here.
}
// Returns a boolean — whether the encoder has reached the target
public boolean atTarget() {
return motor.getPosition().getValueAsDouble() >= targetPosition;
}
// Returns a double — the current motor output
public double getSpeed() {
return motor.getDutyCycle().getValueAsDouble();
}
Tip
Use the return keyword to send a value back to the code that called this method. The type after return must match the declared return type. A void method may use return; (no value) to exit early.
Parameters#
Parameters are inputs you pass to a method when calling it. You can have zero or more, separated by commas.
// No parameters
public void stop() {
motor.set(0);
}
// One parameter — the speed to set
public void setSpeed(double speed) {
motor.set(speed);
}
// Two parameters
public void setLeftRight(double leftSpeed, double rightSpeed) {
leftMotor.set(leftSpeed);
rightMotor.set(rightSpeed);
}
// No parameters
public void stop() {
motor.setControl(new DutyCycleOut(0));
}
// One parameter — the speed to set
public void setSpeed(double speed) {
motor.setControl(new DutyCycleOut(speed));
}
// Two parameters
public void setLeftRight(double leftSpeed, double rightSpeed) {
leftMotor.setControl(new DutyCycleOut(leftSpeed));
rightMotor.setControl(new DutyCycleOut(rightSpeed));
}
Note
Parameters are local variables — they only exist inside the method and disappear when the method finishes.
Method Overloading#
You can have multiple methods with the same name but different parameters. This is called method overloading. The compiler determines which method to call based on the number and types of arguments you provide.
Method Overloading
In this example, the setSpeed method is overloaded to accept either a single double, which tells the motor what speed to run at, or a double and a boolean, which controls whether to reverse the direction of the speed.
Method overloading can make your code more flexible and easier to read, as you can provide different ways to call the same logical operation without needing to come up with different method names for each variation. in FRC, method overloading is often used for drivetrain control, allowing you to set speed with different levels of detail (e.g., just speed, or speed plus direction).
Calling a Method#
To run a method, write its name followed by parentheses containing any required arguments:
// Calling a void method
drivetrain.setSpeed(0.5);
// Calling a method and storing the return value
boolean done = shooter.atTarget();
// Using a return value directly in a condition
if (shooter.atTarget()) {
shooter.stop();
}
FRC Example
In a command's execute() method, calling the drivetrain subsystem to move the robot:
@Override#
When a class extends a parent class (see Java Classes), the @Override annotation can replace one of the parent's methods with its own version. The annotation marks in a clear way that a method is being overridden.
@Override
public void periodic() {
// This replaces the empty periodic() in SubsystemBase
// and runs every robot loop cycle
}
Note
@Override is optional but strongly recommended — if you misspell the method name, the compiler will warn you that there is no matching method to override, catching the typo before it becomes a bug.
Methods Calling Other Methods#
Methods can call other methods, including methods of other objects. This is how FRC subsystems and commands interact.
Tip
Methods that do one thing and do it well are easier to test, reuse, and understand. If a method is getting long, consider splitting it into smaller helpers.
Knowledge Check#
Quiz results are saved to your browser's local storage and will persist between sessions.
What does a void return type mean?
What is the main benefit of adding @Override when replacing a parent class method?
What are parameters in a method declaration?
If a method does not return a value, its return type should be:
Quiz Progress
0 / 0 questions answered (0%)
0 correct