You're seeing this because you're a teacher 🤓

🎯 Your main goal for this lesson

Help students understand the purpose of a function is doing something within the code. The basic formula for a function is to accept something, do things with it, and then return something to the program/user.

🧠 Things to keep in mind when teaching

Like our first foray into CSS, this lesson is sure to cause some frustration. For students that don't have any previous experience in CS, functions can seem like a really abstract concept. Couple that with the fact that we haven't 'done anything' with a webpage in a few lessons, some students may lose site of why they're learning about this. Rest assured, we'll be back to involving CSS and HTML in no time!

🚀 Lesson Plan

Activity One: 20 minutes

Ask students to put themselves in the shoes of a teacher. They've just given a multiple choice test and have made an answer key. Ask them, "What is the process, from start to finish, for a teacher with a stack of twenty tests? Can you write an algorithm - or a process - by which the teacher could follow the instructions and grade every paper?"

The goal of this activity is to help them to understand that a function works by taking in information (a test), applying logic (checking each answer), and then returning information (grade). A teacher 'loops' over each test in the stack and then the 'program' is done.

NB: It can be really fun to have students diagram this instead of writing it out in paragraph form or bullets.

Activity Two: 30 minutes

Lead students through the lesson and provide support as they begin work on their assignments.

Activity Three: 10 minutes

Take the formative assessment at the end of class. Work towards mastery.

Functions

By the end of this lesson, you'll be able to...

  • Identify a function/method.
  • Identify an argument.
  • Declare a function.
  • Identify an arrow function expression.
  • Invoke a function.

Functions

Functions are the actual actions wherein we'll be doing thins with our data. There's a few ways to write them that we'll see below; as we move into more complex interactions, we'll see different patterns emerge for the best way of declaring a function.

Syntax

Functions are composed of a method, or the action and a series of arguments that we feed the function. You've already seen one a number of times in this course: console.log();

example of a function with method and argument highlighted

log(); is a function that's called on the console object. The information we've been printing out is the argument to the function.

1console.log(`Hello, World!`); // returns Hello, World!

We write functions to do different jobs; they contain a series of statements to perform a group of actions. We can write functions a few different ways, but we'll primarily use two in this course.

Function statement

The basic declaration, or creation, of a function is through a function statement using the function keyword.

1function addNumbers(x, y) { 2 let total = x + y; 3 return total; 4}

Functions are said to return information. This means that there's a result of the job with which they're tasked. Typically, this will be written explicitly with a return statement. However, if no return statement is present, the function will return undefined.

In the example above, we return the variable total as a numeric value between from the addition of the arguments x and y.

Arrow function expressions

We can write the same function using an arrow expression. This neatly sets the returned value of our function equal to a variable that we can use later.

1let total = (x, y) => x + y;

More about arguments

Especially depending upon how fast you're working through this information, arguments can seem a bit...weird. An important concept when thinking about functions is that they're what we call abstractions. An abstraction is a model; it's a piece of code that we write that expects certain types of things to be given to it and, if we give it the right information, it will automatically know what to do with it.

Take a look at this example to get the dimensions of a rectangular box. Notice the abstract names for the arguments.

1function getVolume(l, w, h) { 2 let volume = l * w * h; 3 return volume; 4}

Our function, whenever it's invoked, or called, is expecting the first argument to be l, or length; the second argument to be w, or width; and the final argument to be h, or height.

1getVolume(3, 2, 5); // would return 30

If we fail to give it all three arguments, or provide it with too many arguments, we'll get unexpected behaviors that don't result in our intended output.

Also, just because the mathematical formula calls for length, width, and height doesn't mean we have to use those terms as our argument. Our function is simply looking for three arguments to be passed in and then something to be done with them. The following works just the same.

1function getVolume(red, blue, green) { 2 let volume = red * blue * green; 3 return volume; 4}

It can help to think of your functions as blueprints designed to handle certain tasks or problems when needed in your code.


Assignments:

Your teacher may elect to give you this assignment in conjunction with this lesson or at a different time. Pay attention to your teacher's instructions for how/when to complete this assignment!

Practice: Functions


Previous and Next Lessons:

Data Types

Arrays and Array Methods