Java Methods: void, Parameters and return (Complete Beginner)
The design sheet The full architecture and the verified references. Free, no email.
4:31 docker compose up, and watch it happen
A receipt with a dashed line above and below the title prints the same line twice. Change the line and you change it in two places; in a bigger program, twenty. A method gives the instructions a name.
Your first method
void main() {
printLine();
IO.println("Sarah's groceries");
printLine();
}
void printLine() {
IO.println("----------");
}
Java starts in main. When it reaches printLine(), it runs that method’s instructions, then carries on in main
right after the call. void means the method gives nothing back to its caller; it can still print. main is a
method too, sitting side by side with printLine.
Passing a value in
void printCustomLine(String symbol) { IO.println(symbol); } prints whatever it is given:
printCustomLine("==========") and printCustomLine("**********") print two different lines. symbol is the
parameter, the name inside the method; the text in the call is the argument, the actual value.
Getting a result back
int lineTotal(int quantity, int priceCents) {
return quantity * priceCents;
}
int breadTotal = lineTotal(2, 150); stores 300, and adding lineTotal(1, 99) gives 399. return sends a value
back, and the int before the name says what kind.
TypeScript
The same three are functions: function lineTotal(quantity: number, priceCents: number): number. The return type
goes after the brackets, where Java puts it before the name.
The challenge
What does lineTotal(3, 150) + lineTotal(2, 99) print? The answer is in the episode and the repository’s README.
Next episode: arrays.