Bitpath

Lesson 1 · Unit 03

Defining functions

function, parameters and arguments, return against console.log, and undefined

13 exercisesabout 3 min

What you practise

  • function, parameters and arguments, return against console.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?

JavaScript
function greet() {
  console.log("Hello!");
}
greet();
greet();

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.

JavaScript
function greet() {   // define
  console.log("Hello!");
}
greet();             // call

Concept

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.

JavaScript
function add(a, b) {
  return a + b;
}
console.log(add(2, 3));      // 5
console.log(add(2, 3) * 10); // 50

10 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.

One email when Bitpath is ready to try. Nothing else. Privacy