Lesson 1 · Unit 03
Defining functions
function, parameters and arguments, return against console.log, and undefined
What you practise
function, parameters and arguments,returnagainstconsole.log,undefined, one function calling another
Try a question from this lesson
Pick one
Lines 1 to 3 give a block of code the name greet. The last two lines use that name. What do you think is printed?
function greet() {
console.log("Hello!");
}
greet();
greet();Correct answer
Hello! twice
Why
Lines 1 to 3 only define the function: nothing is printed yet. Each greet() then runs the block once, so two calls print two lines. With no calls at all, nothing would be printed.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Define, then call
A function is a named set of instructions.
function greet() { ... } defines it. This stores the instructions under the name. It does not run them.
greet() calls it: the program jumps into the block, runs it, and comes back. You can call a function as many times as you like.
function greet() { // define
console.log("Hello!");
}
greet(); // callConcept
return
return does two things: it ends the function at once, and it hands a value back to the caller.
The call then stands for that value. You can store it, print it, or use it in a bigger calculation.
A function that prints its answer shows it to a person. A function that returns its answer gives it to the program.
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
console.log(add(2, 3) * 10); // 5010 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 1× true or false
- 1× fill the blanks
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.