Functions or Methods allow the programmer to take a bit of logic and encapsulate it to allow it to be easily used. When we build a function, thought should be made towards the reuse of the encapsulated logic.
Doesn't reuse and generalization make the resulting code somewhat larger? Usually it does, but at what cost? For one, improved maintainability of the code. Additionally, the overall code base becomes smaller since several versions of the code are replaced with a one slightly larger one.
OK, so the value of functions can be seen. How do we go about deciding what should be made into a function and what shouldn't? Let's ask the following questions. If any of the answers is yes, it is a candidate. If several are yes, it is a good candidate.
OK, how well did your target code meet the criteria? If none of the questions came up yes, you might still want to write a function. I won't stop you. But if you do, take reuse into account.
Remember the recipe example? We start by deciding what our function should do. Let's start with something simple, like a list of names we want to sort.