# Function signatures

Let's look at how we can add explicit types to our function signatures.

## Parameter types

To add types to your parameter list, simply add annotate the parameter with `: {type}`.

For example:

```typescript
function add(x: number, y: number) {
  return x + y;
}
```

Here TypeScript is actually able to infer that the return type is `number`. Smart! However, we want to be explicit about the return type, so let's annotate it.

## Return type

To add a return type, simply add `: {type}` after the `()` of your parameter declarations.

For example:

```typescript
function add(x: number, y: number): number {
  return x + y;
}
```

Now it's very clear that this function returns a `number` and only a `number`.

If you don't have an explicit return, use [`void`](/typescript/beginner/functions/void-type.md).

## Optional parameters

TypeScript supports optional parameters.

To mark a parameter as optional, simply append a `?` to the parameter's name.

For example:

```typescript
function foo(bar: number, baz?: string): void {
  // ...
}

foo(123); // OK
foo(123, 'hello'); // OK
foo(123, 123); // Error: 2nd parameter not of type 'string'.
foo(123, 'hello', 'extra param'); // Error: expected 1-2 arguments, but got 3.
```

## Default parameters

Additionally, you can provide a default value (using `= someValue` after the parameter declaration) which will get assigned if the caller doesn't provide that argument.

For example:

```typescript
function logGreeting(greeting: string = 'Hello!'): void {
  console.log(greeting);
}

logGreeting(); // logs "Hello!"
logGreeting('Oi como vai!'); // logs "Oi como vai!"
```


---

# 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://shopify-1.gitbook.io/typescript/beginner/functions/function-signatures.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.
