Organizing code into functions
Learn the four parts of a function (name, parameters, return type and body), and how to call one and keep the value it returns.
Apprentice · Java · Updated
Functions are DRY
DRY is a principle in software development that means - DON'T REPEAT YOURSELF. If you have multiple copies of essentially the same code, consider how time consuming it would be to make changes in all copies. Additionally, you would increase the risk of missing some required changes, or possibly making mistakes in some of the copies.
By breaking your code into functions, you minimize this risk.
Creating a function
The basic structure of a function consists of 4 parts:
- The
nameof the function - Input
parameters - The
return type - The function
body
int add(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
Some functions do not return a value.
void displayGreeting(String name)
{
System.out.println("Good Morning " + name);
}
Some functions do not accept input parameters. This one needs import java.util.Random; at the top of the file.
int getRandomNumber()
{
Random numberGenerator = new Random();
return numberGenerator.nextInt();
}
All functions should be focused on doing only one thing. This is called the Single Responsibility Principle (SRP). SRP is a principle of Object-Oriented Programming, but it is also applicable to functions.
Calling a function
To call (run) a function, write its name followed by parentheses: ().
Calling a function that does not return a value is executed on a single line. If the function you are calling requires input parameters, you must provide a value. The value provided is called an input argument.
public static void main(String[] args)
{
displayGreeting("Joseph");
}
public static void displayGreeting(String name)
{
System.out.println("Good Morning " + name);
}
When calling a function that returns a value you should store the returned value into a variable.
public static void main(String[] args)
{
int costOfGum = 3;
int costOfCandyBar = 2;
int totalCost = add(costOfGum, costOfCandyBar);
}
public static int add(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
Do it: a function for one line of the snack bill
The snack stand's register prints one line for each snack a customer buys: the price of one, times how many they bought. Right now that sum is written out by hand every time. Give it a name.
The function gets two values:
price: the price of one snack, adouble(1.25).quantity: how many of that snack the customer bought, anint(4).
For example, 4 packs of gum at $1.25 each:
| Price of one | How many | The line's total |
|---|---|---|
| $1.25 | 4 | 4 × $1.25 = $5.00 |
So lineTotal(1.25, 4) returns 5.0.
Your job: write the function double lineTotal(double price, int quantity) so it returns the line's total for any price and quantity.
- Multiply the price by the quantity, and return the result.
- When the quantity is
0, the line's total is0.0.
The box holds the function with nothing around it. Since Java 25, a small program can be written this way, so the function needs no class and no public static in front of it. Inside a class, as in the lesson, you'd write public static double lineTotal(...).
Practiced live on the lesson page: write the Java right there and check it.