> For the complete documentation index, see [llms.txt](https://hemantajax-2.gitbook.io/typescript-step-by-step/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hemantajax-2.gitbook.io/typescript-step-by-step/function.md).

# Function

* Similar to JavaScript functions&#x20;
* Have some additional features...
  * types
  * default parameters
  * rest parameters&#x20;
  * lambdas

```javascript
function greet(){
    alert("Hello world.");
}
```

## Function Types

* Types describe what is returned by a function

```javascript
function getCountry() : string {
    return "Canada";
}
var c : string = getCountry(); // ok
var c : number = getCountry(); // compile-time error
```

## Optional Parameters

```javascript
function sayHappyBirthday(name?: string){
    console.log("Happy Birthday " + name);
}

sayHappyBirthday(); // OK
sayHappyBirthday("Hemant"); // OK
```

## Default Parameters

```javascript
function sayHappyBirthday(name?: string = "Buddy!"){
    console.log("Happy Birthday " + name);
}

sayHappyBirthday(); // Happy Birthday Buddy!
sayHappyBirthday("Hemant"); // Happy Birthday Hemant
```

## Rest Parameters

* In JavaScript list of all arguments are provided in the mysterious arguments variable&#x20;
* it's real array ( so we can use array methods push, pop etc on it unlike arguments )

```javascript
function roleCall(master: string, ...names : string[]){
    console.log("Master is "+ master); // Master is Hemant
    console.log(names);  // ["Rickon", "Sansa", "Robb", "Arya"]
};
roleCall("Hemant","Varun","Vinay","Sasis","Arya");
```

## Lambdas ( arrow function )

* A different kind of function invocation where the value of this is not changed&#x20;
* Works similar to function but has a preferable syntax&#x20;
* Also known as an arrow or a fat arrow function

```javascript
var superhero = {
    name: "Hemant",
    greet: function() {
        setTimeout(() => {
            console.log(this.name); // Hemant
        }, 500);
    }
};

superhero.greet();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://hemantajax-2.gitbook.io/typescript-step-by-step/function.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.
