# Functions: Declarations, Expressions & Arrows

> The building blocks of JavaScript. Learn the three ways to write functions, how they receive data, and how they return results.

Subject: JavaScript | Lesson 12 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/functions-basics
Previous: [Iterables & the Iteration Protocol](https://codewithanimation.in/javascript/iteration-protocols.md) | Next: [Hoisting & the Temporal Dead Zone](https://codewithanimation.in/javascript/hoisting.md)

## What is a function?

A function is a reusable block of code designed to perform a particular task. You define it once, and you can 'call' or 'invoke' it as many times as you want.

Functions take inputs (called parameters), do some work, and then output a result (via the `return` keyword).

## The three ways to write them

1. **Function Declarations**: `function add(a, b) { ... }`. These are 'hoisted', meaning you can call them before they appear in the code.

2. **Function Expressions**: `const add = function(a, b) { ... }`. Here, the function is anonymous and assigned to a variable. It is not hoisted.

3. **Arrow Functions**: `const add = (a, b) => { ... }`. Introduced in ES6, these are shorter and have special rules regarding the `this` keyword (which we will cover in advanced topics).

## Parameters vs. Arguments

These terms are often mixed up, but they mean different things.

**Parameters** are the variables listed in the function's definition. For example, in `function greet(name)`, `name` is the parameter.

**Arguments** are the actual values you pass to the function when you call it. In `greet('Alice')`, `'Alice'` is the argument. The argument binds to the parameter when the function runs.

## Common mistake: The implicit undefined return

If a function does not have a `return` statement (or if it just says `return;`), it will implicitly return `undefined`. If you ever see a variable unexpectedly holding `undefined` after calling a function, check if you forgot to return a value!

## Tip: Implicit return in Arrow Functions

If an arrow function has only one line of code, you can omit the curly braces `{}` and the `return` keyword. For example: `const double = (x) => x * 2;`. It will automatically return the evaluated result.

## Try it: The Function Flow

Watch how arguments flow into a function's parameters, get processed, and then flow back out via the return statement.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/functions-basics

## Check yourself

### Question 1

Which type of function is 'hoisted' to the top of its scope, allowing you to call it before it is defined?

- A. Function Declaration
- B. Function Expression
- C. Arrow Function

Correct answer: A

Function Declarations (`function name() {}`) are hoisted by the JavaScript engine, meaning they are loaded into memory before any code executes.

- Why not B: Incorrect. Expressions assigned to `let` or `const` cannot be used before initialization.
- Why not C: Incorrect. Arrow functions are assigned to variables, so they are not hoisted like declarations.

### Question 2

In the code `multiply(5, 10)`, what are `5` and `10` called?

- A. Arguments
- B. Parameters
- C. Variables

Correct answer: A

The values you pass into a function when calling it are called Arguments. The variables defined in the function signature to receive those values are called Parameters.

- Why not B: Incorrect. Parameters are the variables named inside the function definition.
- Why not C: Incorrect. They are literal values being passed as arguments.

### Question 3

What does a function return if there is no `return` keyword used inside it?

- A. undefined
- B. null
- C. It throws a SyntaxError

Correct answer: A

By default, any function that does not explicitly return a value will implicitly return `undefined` when it finishes executing.

- Why not B: Incorrect. It defaults to undefined, not null.
- Why not C: Incorrect. Functions are not required to return anything.

## Remember this

- Function Declarations are hoisted; Expressions and Arrow Functions are not.
- Parameters are the names in the definition; Arguments are the values passed during the call.
- Functions without a `return` statement implicitly return `undefined`.
- Arrow functions offer a concise syntax and can implicitly return values if written on one line.
