Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.
A function is an independent block of code that performs a specific task, while a function expression is a way to store functions in variables.
Here's a quick example of function and function expression. You can read the rest of the tutorial for more.
Example
// Create a function named greet()
function greet(): void {
console.log("Hello World!");
}
// Store a function in the displayPI variable
// This is a function expression
let displayPI = function(): void {
console.log("PI = 3.14");
}
// Call the greet() function
greet();
// Call the displayPI() function
displayPI();
// Output:
// Hello World!
// PI = 3.14
Here, we created the greet()
function and used the displayPI variable to create a function expression. Then, we called the functions by using their names followed by parentheses ()
, i.e., greet()
and displayPI()
.
Create a TypeScript Function
We can create a function in TypeScript using the function
keyword and specifying a return type:
function greet(): void {
console.log("Hello World!");
}

Here, we have created a simple function named greet()
that prints Hello World!
on the screen.
Our function contains the following parts:
- Function Keyword - The
function
keyword is used to create the function. - Function Name - The name of the function is
greet
, followed by parentheses()
. - Return Type (Optional) - The data type of the value that is returned by the function. The return type of
greet()
isvoid
, which means it doesn't return any value. - Function Body - The code that is executed when we call the function. In our case, it is
console.log("Hello World!");
Suppose you need to write a program to draw a circle and color it. You can create two functions to solve this problem:
- A function to draw the circle.
- A function to color the circle.
From this example, we can see that functions provide the following benefits:
- Reusable Code: Since functions are independent blocks of code, you can declare a function once and use it multiple times. For example, once you create a function to draw a circle, you can use it whenever you need to draw a circle.
- Organized Code: Dividing small tasks into different functions makes our code easy to organize.
- Readability: Functions increase readability by reducing redundancy and improving the structure of our code.
Call a Function
Previously, we declared a function named greet()
:
function greet(): void {
console.log("Hello World!");
}
If we run the above code, we won't get any output. But why?
It's because creating a function doesn't mean we are executing the code inside it. In other words, the function is ready and available for us to execute whenever we choose.
And if we want to use the function, we need to call it.
Here's how we can call the function:
greet();
As you can see, we call a function by writing the function name (greet
) followed by parentheses ()
.
Example 1: TypeScript Function Call
// Create a function
function greet(): void {
console.log("Hello World!");
}
// Call the function
greet();
console.log("Outside function");
Output
Hello World! Outside function
In the above example, we created a function named greet()
. Here's how the control of the program flows:

Here,
- When the
greet()
function is called, the program's control transfers to the function definition (the place where we've defined the function). - All the code inside the function is executed (
Hello World!
is printed). - The program control then jumps to the next statement after the function call (
Outside function
is printed).
TypeScript Function Arguments
Arguments are values you pass to the function when you call it.
// Function with a string parameter called 'name'
function greet(name: string): void {
console.log(`Hello ${name}`);
}
// Pass argument to the function
greet("John");
// Output: Hello John
In the above example, we passed "John"
as an argument to the greet()
function.

Notice the name variable declared inside parentheses:
function greet(name: string): void {
// Code
}
Here, name is a function parameter, which acts as a placeholder to store the function argument.
In other words, the argument "John"
is stored in the name parameter.
Remember: A function argument is the value we pass to the function, while a function parameter is a placeholder that stores the argument passed to the function.
Pass Different Arguments to the Function
We can pass different arguments in each call, making the function reusable and dynamic.
function greet(name: string): void {
console.log(`Hello ${name}`);
}
// Pass "John" as argument
greet("John");
// Pass "David" as argument
greet("David");
Output
Hello John Hello David
Example 2: TypeScript Function to Add Two Numbers
We can also pass multiple arguments to a single function. For example,
// Function with two arguments
// We haven't specified the return type to make the code clean
function addNumbers(num1: number, num2: number) {
let sum = num1 + num2;
console.log(`Sum: ${sum}`);
}
// Call function by passing two arguments
addNumbers(5, 4);
// Output:
// Sum: 9
In the above example, we have created a function named addNumbers()
with two parameters of number
type: num1 and num2. Here,
- num1 takes the value of the first argument, 5.
- num2 takes the value of the second argument, 4.
The function then adds the values of num1 and num2 and the result is printed as output.

The return Statement
We can return a value from a TypeScript function using the return
statement.
// Function to find square of a number
function findSquare(num: number) : number {
// Return the square of num
return num * num;
}
// Call the function and store the result
let square = findSquare(3);
console.log(`Square: ${square}`);
Output
Square: 9
In the above example, we have created a function named findSquare()
. The function accepts a number and returns the square of the number.
Since the square of a number is also a number, we've specified the return type of findSquare()
as number
.
Here's how our program works:
- First, we passed 3 as an argument to the function.
- So, the function returns the square of 3 (which is 9) to the function call. Thus, the return value is stored in the square variable.
- Finally, the code after the function call is executed. In our case, it's the code to print the value of square.
This process is shown in the image below:

Type Inference in TypeScript
Like JavaScript, TypeScript can infer the return type of a function based on its return statement. For example,
// Function without explicit return type
function findSquare(num: number) {
// Return the square of num
return num * num;
}
// Call the function and store the result
let square = findSquare(3);
// Print the data type of the return value
console.log(typeof square);
// Output: number
This program didn't cause any errors because TypeScript automatically inferred that the return type of the findSquare()
function is number
.
Similarly, TypeScript can also automatically infer that a function that doesn't return any value is of void
type.
// Define a function defining the return type explicitly
function findSquare(num: number) {
// Print the square
console.log(num * num);
}
// Call the function
findSquare(3);
// Output: 9
TypeScript can infer the data type of your function parameters based on the arguments passed and how they're used in the function body. For example,
// Create a function without defining the parameter type
function greet(name): void {
console.log(`Hello ${name}`);
}
// Pass string "John" as argument
greet("John");
// Pass number 5 as argument
greet(5);
Output
Hello John Hello 5
As you can see, this program doesn't cause any errors. However, we recommend you explicitly define the types so as to avoid unnecessary runtime errors.
The return Statement Terminates the Function
Any code written in the function after the return
statement is not executed. For example,
function display(): string {
console.log("This will be executed.");
return "Returning from function.";
console.log("This will not be executed.");
}
let message: string = display();
console.log(message);
Output
This will be executed. Returning from function.
In this example, the display()
function doesn't execute the second console.log()
statement inside it.
This is because the function execution stops at the return
statement. So, the following code is never reached:
console.log("This will not be executed.");
This is what actually happens:
- First, the function prints
This will be executed.
to the screen. - Then, it returns the string
"Returning from function."
to the function call. - Finally, the function terminates its execution.
- The return value is then stored in the message variable and printed.

TypeScript Library Functions
TypeScript can utilize JavaScript's built-in functions that can be directly used in our program. We don't need to create these functions; we just need to call them.
Some common TypeScript library functions are:
Library Function | Description |
---|---|
console.log() | Prints output to the console. |
Math.sqrt() | Returns the square root of a number. |
Math.pow() | Returns the power of a number. |
toUpperCase() | Returns the string converted to uppercase. |
toLowerCase() | Returns the string converted to lowercase. |
To learn more about library functions, visit JavaScript Library Functions.
Example 3: TypeScript Library Function
// Math.sqrt() computes the square root
let squareRoot: number = Math.sqrt(4);
console.log(`Square Root of 4 is ${squareRoot}`);
// Math.pow() computes the power
let power: number = Math.pow(2, 3);
console.log(`2 to the power of 3 is ${power}`);
// toUpperCase() converts text to uppercase
let band: string = "Iron Maiden";
let bandUpper = band.toUpperCase();
console.log(`Favorite Band: ${bandUpper}`);
Output
Square Root of 4 is 2 2 to the power 3 is 8 Favorite Band: IRON MAIDEN
Here,
Code | Action |
---|---|
Math.sqrt(4) |
Calculates the square root of 4, resulting in 2. |
Math.pow(2, 3) |
Computes 2 ^ 3 (2 raised to the power of 3), which is 8. |
band.toUpperCase() |
Converts the string in the band variable to uppercase, resulting in IRON MAIDEN . |
Function Expressions
In TypeScript, a function expression is a way to store functions in variables. For example,
// Store a function in the square variable
// The function takes an argument of number type
// And, it returns a number value
let square = function(num: number): number {
return num * num;
};
console.log(square(5));
// Output: 25
In this example, the function that calculates the square of a number is assigned to the square variable.
We then used this variable to call the function expression using the code square(5)
, where 5 is the function argument.
Note: Like with functions, we need to use parentheses ()
with the variable name to call a function expression.
The Function Keyword
We can use the Function
keyword to create a function expression. For example,
// Declare a variable of Function type
let square: Function;
// Turn square into a function expression
square = function(num: number) {
return num * num;
};
console.log(square(5));
// Output: 25
Here, we're letting TypeScript know that square
is a variable that can only store functions, i.e., we're specifying its type as Function
.
We then defined the function expression later in the program.
Also Read: