Lesson 2 of 8
Functions
- 12 min
- 4 checks
- 3 hints
- Go to task
Goal
A function that takes a value and returns a result
Explanation
A function is a named piece of code: you give it values and it returns a result. The point is that you write it once and then call it whenever you need it.
So if you define function sum(a, b) { return a + b; } and then write sum(2, 3), the result is 5.
Many beginners mix up return and console.log. The difference is that log prints the value so you can see it, while return hands it to the code that called the function so that code can use it. That's why a function that prints its result without returning it is no use to the rest of the program.
Your task
Write the function greet(name) so it returns a greeting that includes the name, then make the button show its result in #out.
Checks
4 checks- The greet function is defined
- It returns a string that includes the name it was given
- It works with any name, not just one hard-coded inside it
- The button shows the function's result in #out