# Functions

Functions are one of the most powerful and essential notions in programming. Functions like mathematical functions perform transformations, they take input values called **arguments** and **return** an output value. &#x20;

Functions can be created in two ways: using `function declaration` or `function expression` . The *function name* can be omitted in f`unction expression` making it an `anonymous function`.  Functions, like variables, must be declared. Let's declare a function `double` that accepts an *argument* called `x` and **returns** the double of x :

```javascript
// an example of a function declaration
function double(x) {
  return 2 * x;
}
```

> *Note:* the function above **may** be referenced before it has been defined.

Functions are also values in JavaScript; they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments :

```javascript
// an example of a function expression
let double = function (x) {
  return 2 * x;
};
```

> *Note:* the function above **may not** be referenced before it is defined, just like any other variable.

{% hint style="info" %}
&#x20;A callback is a function passed as an argument to another function.
{% endhint %}

An arrow function is a compact alternative to traditional functions which has some semantic differences with some limitations. These function doesn't have their own bindings to `this`, `arguments` and `super`, and cannot be used as constructors. An example of an arrow function.

```javascript
const double =  (x) =>  2 * x;
```

{% hint style="warning" %}
The `this` keyword in the arrow function represents the object that defined the arrow function.&#x20;
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://js201.gitbook.io/js-101/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
