498
Control Lab FRC Programming Curriculum
Fundamentals · L06 of 8

Functions

Prereqs: fundamentals-05
Objectives 0 / 3

Hook

You write code to limit motor output to a safe range. It works, so you copy it into the arm subsystem, then the shooter subsystem, then the climber subsystem:

// In arm:
if (power > 1.0)  power = 1.0;
if (power < -1.0) power = -1.0;
armMotor.set(power);

// In shooter — copied:
if (power > 1.0)  power = 1.0;
if (power < -1.0) power = -1.0;
shooterMotor.set(power);

A week later you realize the safe range should be ±0.8, not ±1.0. You update the arm. You forget the shooter. At your first competition, the shooter overdrive trips a breaker.

The fix is a function: write the logic once, call it everywhere.

Core concept

Key Concept

A function is a named, reusable block of code. You define it once. You call it by name from anywhere. Fix it once — fixed everywhere.

Walk-through

In Java, a function is called a method. Here’s how to define and call one:

// Definition: named "clamp", takes three doubles, returns a double
double clamp(double value, double min, double max) {
    if (value < min) return min;
    if (value > max) return max;
    return value;
}

// Calling it:
double safe = clamp(power, -0.8, 0.8);
armMotor.set(safe);

Vocabulary:

  • Parameter — a named input slot in the definition (value, min, max).
  • Argument — the actual value passed when calling (power, -0.8, 0.8).
  • Return value — what the function hands back to the caller.

The caller doesn’t know or care how clamp works — only that it takes a value and two bounds and returns a bounded value. That’s the contract.

Note

void methods don’t return a value. setMotor(double power) sets a motor and returns nothing. Methods that compute something (like clamp) should return a result instead of modifying a field — it makes them easier to test and reuse.

Interactive demo

Trace a call to clamp(1.4, -0.8, 0.8) step by step:

Code Tracer
01clamp(1.4, -0.8, 0.8)// call begins
02if value < min? (1.4 < -0.8)// false
03if value > max? (1.4 > 0.8)// true!
04return max// returns 0.8
05result = 0.8// caller receives
State
value1.4
min-0.8
max0.8
result?

Step through to see values update.

Initial state

Try it yourself

⚡ Try it yourself

Predict the return value of each clamp call without tracing step by step.

double clamp(double value, double min, double max) {
    if (value < min) return min;
    if (value > max) return max;
    return value;
}
⚡ Check your understanding

What does clamp(-0.3, -0.8, 0.8) return?

⚡ Check your understanding

What does clamp(-1.2, 0.0, 1.0) return?

Key takeaways

  • A function defines a contract: given these inputs, produce this output.
  • Parameters are named slots in the definition. Arguments are the values you pass when calling.
  • return immediately exits the function and hands a value back to the caller.
  • Extract logic into a function when the same code appears more than once, or when a block is complex enough that naming it makes the caller easier to read.

Common confusions

“I changed the parameter inside the function — why didn’t the caller’s variable change?” Java passes primitives (int, double, boolean) by value — the function gets a copy. Changing value inside clamp doesn’t affect whatever variable the caller passed in.

“My function compiles but returns the wrong thing.” Add a System.out.println right before each return to see which one is actually executing. Trace it like the CodeTrace above.

Challenge

⚡ Try it yourself

A joystick axis returns values from -1.0 to 1.0, but small movements near zero are just noise. Write a deadband function: if the absolute value of the input is less than a threshold, return 0.0; otherwise return the input unchanged.

Code EditorJavaCtrl+Enter to run
Stuck? Show hint

Math.abs(input) gives you the absolute value. Compare that to threshold.

What’s next

In Lesson 06, we’ll look at debugging — systematic strategies for finding out what your program is actually doing versus what you think it’s doing.