Defining and Calling Functions
Why we want functions
Here is a program that prints a two-line weather summary for one town. Now suppose it has to do the same for a second town. One way is to copy the whole block and change the values at the top.What a function is
A function is a named group of instructions that accomplishes one specific task. If we want that task performed, we call the function by its name. A function may be called as many times as we like, and the instructions inside it run again every single time.In some other languages the same idea is called a procedure or a method. AP CSP calls the idea procedural abstraction: once a task has a name, the rest of the program can ask for the task by name and stop thinking about the steps inside it.
Defining a function with the function keyword
Python writes def and marks the body by indenting it. JavaScript writes function and wraps the body in curly braces. There is no colon on the first line, and the indentation is there only for human readers: the braces are the part the language actually reads.A function may have parameters, which are input variables that carry information into the function so it can do its job. Parameters are what let one definition be reused across a range of input values.
Factoring out the weather summary
Here is that repeated block turned into a function named forecast, with the two values that changed pulled out as parameters.The block beginning with the function keyword is the function definition. The values written in the parentheses at a call are the arguments, and they are copied into the parameters for the length of that one call.
Each call builds its parameters fresh and releases them the moment the call finishes. The second call above knows nothing at all about the first one.
Defining is not running
The definition on its own prints nothing. It is a description of work to be done later, filed away under a name. Nothing between the braces happens until a call goes looking for that name.Flow of control
A program runs from top to bottom, but a call is a detour. When JavaScript reaches a call it jumps into the body, runs the body to the end, then comes back to the point just after the call and carries on from there.| Line reached | What JavaScript does |
|---|---|
| the whole function block | Files the body away under the name. Prints nothing, no matter how many lines the body has. |
a call such as forecast("Dover", 71); | Copies the arguments into the parameters, jumps in, runs the body to the end. |
| the line written just below that call | Runs only after the whole body has finished, never during it. |
Calling before the definition
In Python a call has to come after the def, or the name does not exist yet. JavaScript is different. Before it runs a single line, JavaScript scans the whole file and files away every function declaration it finds. This is called hoisting, and it means a call written above its own definition still works.The program below defines a function with two parameters and calls it twice. Run it, then change it and run it again:
• Add a third call, with a guest and a table number of your own, below
greetGuest("Kenji", 11);.• Press Reset, then delete the two calls.
• Press Reset, then cut the line
greetGuest("Rosa", 6); and paste it at the very top of the program.function greetGuest(name, table) {
console.log("Welcome, " + name);
console.log(name + " sits at table " + table);
}
console.log("Doors open");
greetGuest("Rosa", 6);
greetGuest("Kenji", 11);
console.log("Dinner is served");
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.