[music]
Chris: One of the most important concepts in computer science
is how to define new commands, how to add your own words to a computer language.
Most computer languages have only about 100 words, or commands,
and really the art and magic is in defining your own new words out of these building blocks.
We do this all the time in sports.
For example, in basketball, you start by learning how to dribble,
how to do a layup, how to rebound.
Once you've learned these really basic moves, you learn new moves
and put together these building blocks, like a pick and roll or a give and go.
And you can then go from there to do more complex plays.
And once you learn that play, and give it a name,
everybody on the team knows how to do it.
Similarly, once you've taught a computer how to do an action once
using these sequence of commands, you can make up your own name for that action.
So it's easy to repeat it later.
When you define your own command and give it a name, that's called a function.
Jasmine: There are two steps to using functions in JavaScript.
First, we need to define our function and then we need to call it.
When you define a new function, you are simply giving a single name
to a set of actions you want the computer to perform.
To call a function, you just type the name of the function followed by a parentheses.
This is actually telling the computer to perform or execute
the set of actions you gave a name to.
You've already been calling functions
when you use the turtle commands.
The only difference is that someone else defined them for you.
Notice in AppLab, the toolbox now has a tab called Functions.
There are two blocks in there, one for defining a new function
and one for calling a function.
Let's make a function that moves the turtle forward twice.
We'll call it move twice.
Drag a new function block out and type in the name you want to give the function.
Now, add the code that you want to run when the function is called.
We'll add two move forward commands and then run our program.
Hey! We defined a function, but nothing happened!
The reason is, we forgot step 2.
We need to call the function.
To call the function, we drag out the other block in the functions tab
and add in the name of our function.
Now that we have both defined and called our function,
the code will actually run and the turtle will move.
Notice that if I also call the function before its definition, it still works.
It just calls the function twice.
Defining a function simply gives a name to a block of code without actually running it.
The definition can actually be anywhere in your program.
The rest of the code will execute line by line, just as before.
Calling a function executes the lines of code inside the function,
and afterwards, the program returns to where it left off when the function call was made.
The fact that the function definition can be placed anywhere
means that your code can get messy and really hard to read.
So we typically put our function definitions at the end of our programs,
to help keep things organized.
In JavaScript, we call them functions. But the generic term is procedure.
All programming languages have a way to define and use new procedures
and it's just another example of how computer scientists
use abstraction to solve problems.
Once you have written a procedural function that you know works as intended,
you can call the function as often as you wish,
without worrying about the details or problems you had to solve to get it working.
And in defining a function to perform a certain task,
you've essentially increased the capabilities of the programming language
and can now use that function on its own
or as a part of even more complex actions.
You can also see why it's important to give your function
a meaningful and descriptive name.
If you call this something like "do stuff",
it would be unclear what the function was supposed to do
and make it hard to use in your own code.
By referencing a large and complex block of code by a single name,
it makes it much easier to understand what a program is doing by reading the code
and also easier to locate and fix problems or errors.