No history yet

Function Basics

What is a Function?

Think of a function like a blender in your kitchen. You put ingredients in, press a button, and it performs a specific task: blending. You can use it over and over without having to rebuild it each time. A function in JavaScript is a reusable block of code designed to perform a particular task.

The main reason we use functions is to avoid repeating ourselves. Imagine you need to print a welcome message in ten different places in your program. Without a function, you'd have to type out the same line of code ten times. If you wanted to change the message, you'd have to find and edit all ten instances. With a function, you write the code once and just “call” it whenever you need it. This core idea is known as the DRY principle—Don't Repeat Yourself.

Building Your First Function

Let's look at the basic structure of a function. It has a few key parts.

function sayHello() {
  console.log("Hello there!");
}

First, we have the which tells JavaScript we are about to define a function. It's a required signal.

Next is the function's name, sayHello. It's best to give functions descriptive names that explain what they do. Notice the use of , where the first word is lowercase and each new word starts with a capital letter. This is a common naming convention in JavaScript.

After the name, you'll see a pair of parentheses (). We'll explore what goes inside them in a later lesson. For now, just know they are required.

Finally, we have the curly braces {}. Everything inside these braces is the function's body—the block of code that runs when the function is used. In this case, it's a single line that prints a message to the console.

Defining vs. Calling

Writing a function is like writing down a recipe. You've defined the steps, but you haven't actually cooked anything yet. Just creating the sayHello function doesn't make the message appear. This is defining the function.

Defining a function is writing the recipe. Calling a function is cooking the meal.

To actually run the code inside the function's body, you must call (or invoke) it. You do this by typing the function's name followed by parentheses.

// 1. Define the function (write the recipe)
function sayHello() {
  console.log("Hello there!");
}

// 2. Call the function (cook the meal)
sayHello(); // This line makes the code run.

When the program reaches sayHello();, it jumps to the function definition, executes the code inside the curly braces, and then returns to where it left off.

Putting It All Together

Let's see why this is so useful. Here’s some code that doesn't use a function to display a daily greeting.

// Without a function
console.log("Good morning!");
console.log("Don't forget your meeting at 10 AM.");

console.log("Good morning!");
console.log("Don't forget your meeting at 10 AM.");

console.log("Good morning!");
console.log("Don't forget your meeting at 10 AM.");

That’s a lot of repetition. If the meeting time changes, you have to edit it in three different places. Now, let’s do the same thing with a function.

// With a function
function showDailyGreeting() {
  console.log("Good morning!");
  console.log("Don't forget your meeting at 10 AM.");
}

showDailyGreeting();
showDailyGreeting();
showDailyGreeting();

The code is cleaner and more efficient. If you need to change the message, you only have to update it inside the function body. You've created a simple, reusable block of code that saves you work and makes your program easier to manage.

Quiz Questions 1/5

What is the primary principle behind using functions in programming?

Quiz Questions 2/5

In a JavaScript function definition, which symbol(s) enclose the block of code that will be executed?