Methods

Estimated time to read: 1 minute

How can one program tackle such a large problem? This is where methods and their ability to accomplish smaller, specific tasks comes in handy. Through method decomposition, we can use methods to break down a large problem into smaller, more manageable problems.

Methods are so reusable. Imagine we wrote a sandwich-making program that used 20 lines of code to make a single sandwich. Our program would become very long very quickly if we were making multiple sandwiches. By creating a makeSandwich() method, we can make a sandwich anytime simply by calling it.

makeSandwich() adds ingredients together to make a sandwich

If we were to share this sandwich-making program with another person, they wouldn’t have to understand how makeSandwich() worked. If we wrote our program well, all they would need to know is that if they called makeSandwich(), they would receive a sandwich. This concept is known as procedural abstraction: knowing what a method does, but not how it accomplishes it.