More About Functions

Created By: Geoffrey Challen
/ Updated: 2021-09-06

Today's 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!

Tracing Function Execution

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.

Walk through function flow slowly and carefully using a single function example. Go line by line pointing out what happens when, and how the program jumps around.

Next, let's think about what happens when one of our functions itself calls another function!

Walk through function flow with a more complex code path. Emphasize how, each time a function is called, the caller waits for the method to complete before proceeding. Generalize this to functions calling functions calling functions.

Common Algorithm Patterns

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.

Discuss how to modify the basic accumulator pattern to count even values, count odd values, and even perform a sum.

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.

Discuss the basic search function template. Modify to something like allEven or allOdd and discuss short circuit by counterexample.

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!

Method Overloading

Perhaps you noticed something unsettling about the example that starts this lesson:

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.

Discuss method overloading and the notion of a method signature.

void Return

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 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:

Discuss void and give some examples.

Enhanced for Loop

As a final nugget to close this lesson, we'll return to patterns. Remember how we started with this common while loop:

and eventually arrived at this common for loop:

Well, that for loop became so common that there's an even simpler way to work through the values in array using Java's enhanced for loop:

Show off the enhanced for loop or foreach loop.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough