Functions · Lesson 14 of 45
Functions
Write code once, use it many times.
9 minutes3 games
A function is named code that takes inputs (parameters) and can give back a result with return. The values you pass when calling it are arguments. A function exits as soon as it reaches return.
Step by step1 / 5
- ▶function area(width, height) {
- 2 return width * height
- 3}
- 4const a = area(4, 5)
- 5console.log(a)
Variables
—
Output
…
This only defines the function; nothing runs yet.
What's the output?
function double(n) {
return n * 2
console.log("done")
}
console.log(double(4))What will this code print?
Complete the code
Complete the function that returns the bigger of two numbers.
function max(a, b) { if (a > b) { a } return }
Exercise
Write greet(name) that returns Welcome, <name>!, then print greet("Ali").
Expected output: Welcome, Ali!
JavaScript
function greet(name) {
}
Learn more from
Played 0 of 3 games
Sign in to save your progress and earn XP.