Signed in as
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.
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!
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.
Lead students through the lesson and provide support as they begin work on their assignments.
Take the formative assessment at the end of class. Work towards mastery.
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.
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();
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.
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
.
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;
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.
Practice: Functions