Adding content to 106-functions

content/typescript
syedmouaazfarrukh 2 years ago
parent 94ebd6cc89
commit 411387bda6
  1. 34
      src/roadmaps/typescript/content/106-functions/100-typing-functions.md
  2. 24
      src/roadmaps/typescript/content/106-functions/101-function-overloading.md
  3. 39
      src/roadmaps/typescript/content/106-functions/index.md

@ -1 +1,33 @@
# Typing functions
# Typing Functions
In TypeScript, functions can be typed in a few different ways to indicate the input parameters and return type of the function.
1. Function declaration with types:
```
function add(a: number, b: number): number {
return a + b;
}
```
2. Arrow function with types:
```
const multiply = (a: number, b: number): number => {
return a * b;
};
```
3. Function type:
```
let divide: (a: number, b: number) => number;
divide = (a, b) => {
return a / b;
};
```
Learn more from the following links:
- [More on Functions](typescriptlang.org/docs/handbook/2/functions.html)
- [TypeScript Basics - Typing with functions](https://www.youtube.com/watch?v=do_8hnj45zg)

@ -1 +1,23 @@
# Function overloading
# Function Overloading
Function Overloading in TypeScript allows multiple functions with the same name but with different parameters to be defined. The correct function to call is determined based on the number, type, and order of the arguments passed to the function at runtime.
For example:
```
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
return a + b;
}
console.log(add(1, 2)); // 3
console.log(add("Hello", " World")); // "Hello World"
```
Learn more from the following links:
- [TypeScript - Function Overloading](https://www.tutorialsteacher.com/typescript/function-overloading)
- [Function Overloads](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads)

@ -1 +1,38 @@
# Functions
# Functions
Functions are a core building block in TypeScript. Functions allow you to wrap a piece of code and reuse it multiple times. Functions in TypeScript can be either declared using function declaration syntax or function expression syntax.
1. Function Declaration Syntax:
```
function name(param1: type1, param2: type2, ...): returnType {
// function body
return value;
}
```
2. Function Expression Syntax:
```
let name: (param1: type1, param2: type2, ...) => returnType =
function(param1: type1, param2: type2, ...): returnType {
// function body
return value;
};
```
For example:
```
function add(a: number, b: number): number {
return a + b;
}
let result = add(1, 2);
console.log(result); // 3
```
Learn more from the following links:
- [Functions](https://www.typescriptlang.org/docs/handbook/functions.html)
- [TypeScript Functions](https://www.w3schools.com/typescript/typescript_functions.php)
- [TypeScript - functions](youtube.com/watch?v=mblaKPWM9NU)
Loading…
Cancel
Save