Learner

Overloading

Remember that we mentioned that a method's name and parameter list are called the method's signature and this signature is how Java identifies methods. So it follows that while methods with a different name are definitely not the same method, methods with the same name and different parameter lists are also not the same method. This allows you to use different parameter lists to create variations of a method single name. This is called overloading. Here is an example of overloading:

Passing Parameters

Parameters (also called arguments) are the way in which we can send data (variables or constants) to a method for it to act on. While there are cases where methods may not need parameters, parameters make methods especially useful. Parameters allow a method to produce a different result each time it is called depending on the input values supplied. Parameters are said to be "passed" to the method.

Returning Data

While methods can perform their function without returning any data to the calling code, a common use of methods is to perform some process or computation and return a result to the caller. When a method does not return a value, the return data type is called void. An example of a method that would have no return value would be a motor’s setPower(x) function. When we use that function, we aren’t looking for the code to compute something and give us the result, but we are asking the method to make some change in the robot hardware’s current running state.

Method Structure

The form of a method is:

The components are:

What are Methods?

Methods are reusable code blocks that are only executed when called from another location in your program. Methods are also called functions or subroutines. For our purposes, these names mean same thing. In other languages, the names may mean something a bit different.

FOR Statement

The for statement is used to execute a block of statements some specific number of times. The general form of the for statement is:

SWITCH Statement

The switch statement allows you to compare a value to a list of possible matching values and so select from multiple possible statements or blocks. A key difference from the if statement is that the value compared is not boolean, but instead numeric, enumeration (discussed later) or String. The form of the switch statement is:

WHILE Statement

Another form of flow control allows us to have a statement (or block of statements) repeatedly executed while a comparison result is true. This is done with the while statement. The form of the while statement is:

Here are some examples, each using a comparison of the value of variable z to a constant to arrive at a boolean value. Assume z = 0 at the start of this code:

IF Statement

Once we are able to compare values (variables) and arrive at a boolean true/false (or yes/no) value, we need a way to act on that boolean value. With comparisons we are essentially asking a question and will change program flow based on the answer. We ask questions with flow control statements, the first of which is if. The general form of the if statement is:

Comparison

A simple program, like Hello World, just flows from start to finish, one statement after another until done. Solving real problems always involves evaluating data or user input and making decisions about what to do next, when to do it and what to do when things go wrong and much more. Handling these possibilities is called Flow Control.

Pages