Skip to main content

Methods

If you haven't picked it up by now programmers are very very lazy. If we have code that is written more than once or could be repeated if template-able we can use a method. Methods are a block of code that perform a specific task or a set of actions. They can help to reduce code duplication, improve readability, and make our code more organized. Methods can be called from anywhere in our code, allowing us to re-use the same code multiple times without having to write it out again. They are a fundamental concept in object-oriented programming. Methods are also called functions, procedures, or subroutines in other programming languages.

Method Syntax

A method is defined by its signature. A method signature contains its return type, method name, and any parameters it may use as inputs. They should be named using camelCase, which means that the first letter of the first word is lowercase and the first letter of each subsequent word is capitalized. For example, calculateSpeed, setMotorSpeed, and getSensorData are all valid method names. The general syntax for a method is as follows:

returnType methodName(){
// method code
}
  • returnType: Datatype of the value the method with return
    • For simple examples imagine the return type as void. This means there is nothing returned.
  • methodName: This can be anything similar to a variable name. Try to name your methods based off what actions they preform.
  • method code: This will be the code that runs when your method is "called".

Below is an example of a simple method.

// Creating the Method
void stopMotors(){
motor_speed = 0;
}

// Calling the Method
stopMotors();

Parameters

Parameters allow us to template our method to allow for different inputs. The values when passed into a method called arguments. hey allow us to re-use our method with different arguments each time we call them. Methods can have as many parameters as you would like including none which you saw in the previous example. Each parameters need a datatype and a name to be referenced by within its scope.

// Creating the Method
void printInfo(String motor, double speed){
System.out.println(motor + ": " + speed);
}

// Calling the Method
printInfo("Shooter Motor", 3.0);
Method Scope

Your parameters only exist in a method's scope. That means outside of the {} of your method they do not exist and cannot be referenced. This goes for any variables you create inside your method as well.

// Neither param_name or var_name can be referenced here
void methodName(double param_name){
double var_name;
}
// Neither param_name or var_name can be referenced here
tip

You have seen curly braces {} in several parts of the code already. There are used in methods, if statements, classes, etc. This is actually the creation of a scope. They function the same way as described above. Any variables within your scope can only be referenced from inside that scope. You can declare a scope anywhere in your code if you were so inclined.

Return

We can also have our methods produce an output. The examples above have use void as their return type. This means that they have nothing to return and are just executing code. If we change that return type to one of our datatypes we can make our method "return" something of that type.

// Creating the Method
int sum(int a, int b){
return a + b;
}

// Calling the Method
int output = sum(5, 2)
Ending the Method

Any code in a method after the return will not be run.

int sum(int a, int b){
int output = a + b;
return output;
System.out.println(output); // This code will not run!
}
Multiple Returns

Methods can have multiple return statements as long as they are in different "branches" of logic within the code.

boolean isNumberEven(int number){
if(number / 2 == 0){
return true;
} else {
return false;
}
}

Lambdas

Similar to methods, lambda expressions, (also called lambdas, lambda functions, or arrow functions) are a specific type of method that can go unnamed, take in some or no parameters, and produce an output. However, they cannot initialize variables or contain statements such as if or for. These are less commonly used in programming, but can be helpful in specific situations.

Syntax
(type n1, type n2) -> {body of expression}
(int a, int b) -> {a + b};
Example
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
numbers.forEach( (n) -> { System.out.println(n); } );
info

Within our robot code we typically try to keep lambdas limited to commands interaction. Interfacing with buttons is typically done with lambdas.