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.
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 FunctionWrite the function and its task.
Step 2
Pass ParametersProvide input values when needed.
Step 3
Execute FunctionRun the statements inside the function.
Step 4
Return ResultReturn 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
-
functionis used to declare a function. -
welcomeis the function name. -
voidindicates 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.