Skip to main content
🌱 Programming for Complete Beginners

Your First Function

You just read code β€” now you'll write it. This is going to be fun. 🌱

Here's the heart of it: a value you can name and hand back. You wrap that up in a little machine called a function. You give the machine a name, and when someone runs it, it hands a value back to them. Handing a value back is called returning.

Here's that same tiny machine that always returns the number 7:

function lucky() {
  return 7;
}

Read it slowly:

  • function says "I'm building a machine."
  • lucky is the machine's name.
  • return 7 means "hand back the value 7."

Your task

Write a function called answer that returns the number 42. (Just fill in what's inside the machine β€” don't worry about the rest yet.)

⚑ 15 XP

🧱 Code blocks

Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.

solution.jsRuns in your browser
Loading editor…
Tests1 to pass
answer() gives back 42

Run your code to check these.