Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

Introduction to Functions in TypeScript

Learn how to create reusable functions, pass parameters, return values, use arrow functions, and work with advanced function features in TypeScript.

Simple Explanation: A function is a reusable block of code that performs a specific task. Instead of writing the same code repeatedly, we can create a function and call it whenever required.

What Will We Learn?

Functions help us organize code into smaller, reusable blocks. TypeScript also supports parameter types, return types, optional parameters, arrow functions, and function overloads.

  • Create and call functions.
  • Pass parameters and return values.
  • Use optional and default parameters.
  • Write concise arrow functions.
  • Accept multiple arguments using rest parameters.
  • Define function overloads for different input patterns.

Function Workflow

Follow these steps to understand how a function works in TypeScript.

📝
Step 1
Define Function

Write the function and its task.

→
📥
Step 2
Pass Parameters

Provide input values when needed.

→
⚙️
Step 3
Execute Function

Run the statements inside the function.

→
✅
Step 4
Return Result

Return a value when required.

1. Your First TypeScript Function

Let's create a simple function that displays a welcome message.

Example

function welcome(): void {
    console.log("Welcome to TypeScript Functions!");
}

welcome();

Expected Output

Welcome to TypeScript Functions!

Explanation

  • function is used to declare a function.
  • welcome is the function name.
  • void indicates that the function does not return a value.
  • welcome() calls and executes the function.

2. Function with Parameters

Parameters allow a function to receive data when it is called. TypeScript lets us specify the type of each parameter.

Example

function greetStudent(name: string): void {
    console.log("Hello, " + name);
}

greetStudent("Rahul");
greetStudent("Priya");

Expected Output

Hello, Rahul
Hello, Priya

The same function can be reused with different input values.

3. Function with a Return Value

A function can calculate a result and return it to the code that called the function.

Example

function add(a: number, b: number): number {
    return a + b;
}

let result: number = add(10, 20);

console.log("Sum:", result);

Expected Output

Sum: 30

The return type number indicates that the function returns a numeric value.

4. Functions Topics

Explore the following topics in sequence to learn TypeScript functions step by step.

Sr. No. Topic Description Action
1 Function Basics Function declaration, calling, and reusable code. Learn
2 Parameters and Return Types Function inputs, type annotations, and return values. Learn
3 Optional and Default Parameters Work with optional arguments and default values. Learn
4 Arrow Functions Write functions using arrow syntax. Learn
5 Rest Parameters Accept multiple arguments in a single parameter. Learn
6 Function Overloads Define multiple function signatures for different inputs. Learn

Summary

In this introduction, we learned what functions are, how to define and call them, how to pass parameters, and how to return values. In the next lessons, we will explore function features in more detail.