Matthew Ayers

I create my ideas with code.

2 minute read

Functions and Methods

Functions and methods are a necessary part of writing and maintaining code, and to the untrained eye, it might seem pointless to use. However, they have increased efficiency and speed for the developers who write the code, keep it up to date, and for the devices the code runs on.

What Do Functions and Methods Do?

For the average individual in Western society, there is usually a consistent routine for waking up. It may look something like this:

  1. Wake up
  2. Change clothes
  3. Eat breakfast
  4. Brush teeth
  5. Leave home

    While the above steps are self-explanatory in this context, it may seem difficult to explain in more detail. For instance, the process of brushing teeth may be explained like this:

    1. Enter bathroom
    2. Put toothpaste on toothbrush
    3. Brush each tooth
    4. Spit out toothpaste
    5. Rinse off toothbrush
    6. Leave bathroom

      I intentionally left a few steps out for concision, but the process of brushing one's teeth can generally be summed up as that. However, it would make much more sense to just say "brush teeth". The same can be said for writing code.

      People like to summarize. Writers will write a synopsis for a book, researchers will write an abstract for their publications, and software engineers will "abstract" lines of code to make running the same code more efficient. Translating the above scenario into code is fairly simple:

      func wakeUpRoutine() {
          wakeUp()
          changeClothes()
          eatBreakfast()
          brushTeeth()
          leaveHome()
      }
      
      func brushTeeth() {
          enterBathroom()
          putToothpasteOnToothbrush()
          brushEachTooth()
          spitOutToothpaste()
          rinseOffToothbrush()
          leaveBathroom()
      }
      

      It's clear to see through this example how abstracting repeated steps into functions or methods is useful in writing programs. A morning routine is a repeated series of steps, all of which are typically conducted in the same way, and people have developed a certain order for completing each step.

      The Difference Between Functions and Methods

      Text goes here

      Why This Matters

      Text goes here

      Feedback

      If you have any feedback for this post, feel free to contact me so that I know how to improve. Your thoughts are greatly appreciated!

      Tagged with: