A function is a named sequence of instructions written as a unit, that performs a specific task. Functions help make your code organized, arranged, modular, flexible, and nimble/agile.

To define a function (and for the function to work), you’ll need to:

  • Name the function. For example function writeanEssay() {
  • Define the function: Give the function some inputs (called parameters) and instructions to work with
  • Use the function by calling it – that way, the function can execute all of its instructions or steps

Each time you call the function, you’ll give actual values for each input (known as arguments).

How We Use Functions

We use functions to:

  1. reuse (or recall) stuff we’d need later – multiple times,
  2. generalize or make flexible,
  3. organize, group or arrange multiple functions (sometimes within a bigger function)

1. To reuse (or recall):

function writeanArgumentativeEssay() {
   Write headline
   Write introduction
   Develop your arguments
   Write a conclusion
   Edit your essay

2. To generalize

function writeanEssay(thoughts1, thoughts2, thoughts3) {
   Write headline
   Write introduction
   Explain your thoughts1
   Explain your thoughts2
   Explain your thoughts3
   Write a conclusion
   Edit your essay

The function now has a new name: writeanEssay. Also, thoughts1, thoughts2, and thoughts3 are the three inputs (or parameters).

To use the function in this case, we’ll need to call the function with the arguments (no pun intended) that fit with the thoughts.

For example:

function writeanEssay(economy, climate change, general elections)

where economy, climate, and general elections represent the three arguments.

3. To group and arrange our instructions

Here, we have multiple functions within a bigger function.

Each set of related instructions is grouped into its own function.

That way, we can add or remove instructions for each function (as see fit) without messing up with the whole big function; or going through the entire function line-by-line.

Resources: 1

1 thought on “Functions”

Leave a Comment