# Typing Functions

> Parameters, optional arguments, and explicit return types.

Subject: TypeScript | Lesson 18 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/typing-functions
Previous: [Discriminated Unions](https://codewithanimation.in/typescript/discriminated-unions.md) | Next: [Function Overloads](https://codewithanimation.in/typescript/function-overloads.md)

## The core of your logic

In TypeScript, you can add types to both the input parameters of a function and the output value (the return type).

This guarantees that the function receives what it expects, and the caller receives what was promised.

## Preventing silent mistakes

If you don't explicitly type the return value, TypeScript will infer it based on what you return. While inference is smart, explicit return types are safer.

An explicit return type prevents you from accidentally returning early or returning the wrong variable due to a typo.

## Syntax

```typescript
// 1. Parameter Types & Explicit Return Type
function calculateTax(amount: number, rate: number): number {
  return amount * rate;
}

// 2. Optional Parameters (?)
// The 'message' parameter can be string or undefined
function log(id: number, message?: string): void {
  console.log(id, message);
}
```

## Common mistake: Optional parameter ordering

A very common error is placing an optional parameter before a required parameter.

```typescript
// Error: A required parameter cannot follow an optional parameter.
function greet(name?: string, id: number) {}
```

Optional parameters (and parameters with default values) must always come at the *end* of the parameter list.

## Tip: Arrow Function Types

When defining a type alias for a function (like a callback), use arrow syntax: `type Callback = (data: string) => void;`

## Try it: Try it

Toggle the `?` optional flag to see how it affects the function signature and the compiler's strictness.

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

## Check yourself

### Question 1

Why does TypeScript throw an error if you define 'function setConfig(url?: string, port: number)'?

- A. You cannot mix strings and numbers in the same function.
- B. Optional parameters must always appear after all required parameters.
- C. The function is missing an explicit return type.

Correct answer: B

If you call setConfig("http..."), does that map to the url or the port? Because JavaScript parameters are mapped by position, optional parameters must always be at the end.

- Why not A: You can.
- Why not C: Return types are strongly recommended but not strictly required (unless strict mode flags demand it).

## Remember this

- Type parameters by adding `: Type` after the parameter name.
- Type the return value by adding `: Type` after the parentheses.
- Make parameters optional by adding a `?` before the colon.
- Optional parameters must always be at the end of the signature.
