Constituents

Learn what constitutes a method.

We'll cover the following

There are four things that constitute a method:

  • A name
  • A block of code
  • Accepting input (optional)
  • Returning output

Not all methods actually need input (so they don’t take any), and we don’t always care about the output a method returns.

Arguments and return values

Programmers usually don’t use the term “input” in this context. Instead, we say that a method accepts a number of arguments (pieces of input). Instead of “output,” we use the term “return value.” This is what we get back from the method.

Analogy

Imagine a vending machine where we can insert money, press some buttons, and the machine releases the chocolate bar we want.

If the vending machine was a method, then our money, as well as the buttons we press, are the input. The way the machine works internally to release the chocolate bar is the block of code, and the product it dispenses is the return value.

In a sense, we could say that a vending machine transforms money and data into chocolate.

Methods are a lot like that.

Remember: Methods have a name, take some input, do something with it, and return a result.A method’s input is referred to as arguments, while its output is called a return value.

This will become more clear in the following chapters. Let’s define a method next.