Lists Review and Performance : 03/31/2023
Linked Lists : 03/30/2023
Algorithms and Lists : 03/29/2023
Lambda Expressions : 03/24/2023
Anonymous Classes : 03/23/2023
Practice with Interfaces : 03/22/2023
Implementing Interfaces : 03/21/2023
Using Interfaces : 03/20/2023
Working with Exceptions : 03/10/2023
Throwing Exceptions : 03/09/2023
Catching Exceptions : 03/08/2023
References and Polymorphism : 03/07/2023
References : 03/06/2023
Data Modeling 2 : 03/03/2023
Equality and Object Copying : 03/02/2023
Polymorphism : 03/01/2023
Inheritance : 02/28/2023
Data Modeling 1 : 02/27/2023
Static : 02/24/2023
Encapsulation : 02/23/2023
Constructors : 02/22/2023
Objects, Continued : 02/21/2023
Introduction to Objects : 02/20/2023
Compilation and Type Inference : 02/17/2023
Practice with Collections : 02/16/2023
Maps and Sets : 02/15/2023
Lists and Type Parameters : 02/14/2023
Imports and Libraries : 02/13/2023
Multidimensional Arrays : 02/10/2023
Practice with Strings : 02/09/2023
null : 02/08/2023
Algorithms and Strings : 02/07/2023
Strings : 02/06/2023
Functions and Algorithms : 02/03/2023
Practice with Functions : 02/02/2023
More About Functions : 02/01/2023
Errors and Debugging : 01/31/2023
Functions : 01/30/2023
Practice with Loops and Algorithms : 01/27/2023
Algorithms : 01/26/2023
Loops : 01/25/2023
Arrays : 01/24/2023
Compound Conditionals : 01/23/2023
Conditional Expressions and Statements : 01/20/2023
Operations on Variables : 01/19/2023
Variables and Types : 01/18/2023
Welcome to CS 124 : 01/17/2023
More About Functions
void printIt(int count) {
System.out.println(count);
}
void printIt(int first, int second) {
System.out.println(first + second);
}
printIt(10);
printIt(40, 50);
Our next lesson continues our exploration of functions.
We’ll reinforce material covered in our last lesson, and go through some example of exactly what happens during function execution.
We’ll also learn about overloading, when two methods share the same name.
Let’s get started!
While we’ve started to use and even write functions, we may be still a bit fuzzy about exactly what happens when a function runs.
So let’s go through that slowly and carefully using a few examples.
boolean completelyEven(int[] values) {
for (int i = 0; i < values.length; i++) {
if (values[i] % 2 != 0) {
return false;
}
}
return true;
}
System.out.println(completelyEven(new int[] {1, 2, 5}));
System.out.println(completelyEven(new int[] {0, 2, 4}));
Next, let’s think about what happens when one of our functions itself calls another function!
boolean isEven(int value) {
}
boolean completelyEven(int[] values) {
for (int i = 0; i < values.length; i++) {
if (values[i] % 2 != 0) {
return false;
}
}
return true;
}
System.out.println(completelyEven(new int[] {1, 2, 5}));
System.out.println(completelyEven(new int[] {0, 2, 4}));
Computer scientists and programmers love patterns.
Particularly in code.
Frequently, once you’ve learned how to do one thing with code, you can modify that skeleton to solve other similar problems.
Let’s take a concrete example.
The following code determines the count of the number of elements in an array.
Note that this is not a good way to do this—it’s better to just use array.length
.
But we can modify this basic template to solve other problems.
Let’s see how.
int arrayLength(int[] values) {
int len = 0;
for (int i = 0; i < values.length; i++) {
len++;
}
return len;
}
System.out.println(arrayLength({1, 2, 5}));
Searching arrays produces another common pattern.
It’s different from the example above, because we don’t necessarily need to complete the loop.
Again, let’s walk through the basic template and then consider how to expand it.
boolean arrayFindFour(int[] values) {
for (int i = 0; i < values.length; i++) {
if (values[i] == 4) {
return true;
}
}
return false;
}
Through the rest of our lessons we’ll continue to point out patterns that emerge in our programming.
And ways to modify the basic pattern to produce more interesting designs!
Perhaps you noticed something unsettling about the example that starts this lesson:
void printIt(int count) {
System.out.println(count);
}
void printIt(int first, int second) {
System.out.println(first + second);
}
printIt(10);
printIt(40, 50);
Note only do we have two functions with the same name—printIt
—but they both work!
Distinguish this from variables, where we can never use the same name twice in the same scope:
Having multiple methods with the same name is known as method overloading.
We don’t use this feature often, but you may see it in other Java code that you work with.
When two methods have the same name Java must be able to distinguish them from each other when they are used.
So, if two methods have the same name, something must be different about them.
The walkthrough below describes how Java can tell them apart.
void printIt(int count) {
System.out.println(count);
}
void printIt(int first, int second) {
System.out.println(first + second);
}
printIt(10);
printIt(40, 50);
If you have been watching carefully, you’ve already seen something new in this lesson: void
.
When a function doesn’t return a value, you declare it with the void
return type.
Like this:
void printCount(int count) {
System.out.println("The count is " + count);
}
printCount(5);
printCount(8);
void
functions can still use return
, but can’t return a value.
The result of calling a void
function can also not be used in an assignment or statement:
void printCount(int count) {
System.out.println("The count is " + count);
}
printCount(5);
printCount(8);
More Practice
Need more practice? Head over to the practice page.