Chapter 3. Functions: Let’s get organized

image with no caption

As programs grow, the code often becomes more complex.

And complex code can be hard to read, and even harder to maintain. One way of managing this complexity is to create functions. Functions are snippets of code that you use as needed from within your program. They allow you to separate out common actions, and this means that they make your code easier to read and easier to maintain. In this chapter, you’ll discover how a little function knowledge can make your coding life a whole lot easier.

Starbuzz is out of beans!

The Starbuzz buyers love the program you created in the last chapter. Thanks to your efforts, the Starbuzz CEO is only buying coffee beans when the price drops below $4.74, and his organization is saving money as a result.

But, now there’s a problem: some Starbuzz outlets have run out of beans.

image with no caption

When the coffee beans start to run low in an outlet, the Starbuzz baristas need to be able to send an emergency order to the CEO. The outlets need some way of immediately requesting the purchase of coffee beans at the current price, regardless of what that price is. They also need the option of waiting for the best price, too, just like in the current program.

The program needs an extra option.

What does the new program need to do?

The new program ...

Get Head First Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.