Exams Preperation
SESSION 04
TypeScript
FUNCTIONS
A TypeScript function is a
block of code designed to
perform a particular task.
Functions
A TypeScript function is
executed when "something"
invokes it (calls it).
Example
TypeScript
Function Syntax
A TypeScript function is defined with the function keyword,
followed by a name, followed by parentheses ().
The parentheses may include parameter names separated by
commas: (parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly
brackets: { }
Example
Function
Paramters & Arguments
Function parameters are listed inside the parentheses ()
in the function definition.
Function arguments are the values received by the
function when it is invoked.
Inside the function, the arguments (the parameters) behave
as local variables.
{
Parameters
Arguments
{
Types Of
FUNCTIONS
Anonymous
Function
Name Arrow
Function Function
Examples
Named Function Anonymous Function Arrow Function
Traditional way Without name One-lined function
This type receives a name for the This type doesn't receive any name for Arrow functions are been introduced in
function declaration. declaration. Instead, the function will be the ES6 version. It is used to shorten the
assigned to a variable for invoking. code.
Why
Functions
With functions you can reuse code.
You can write code that can be used many
times.
You can use the same code with different
arguments, to produce different results.
@CodeWithHamza
LinkedIn/HamzaMirza
Instagram/codewithhamzaofficial
[email protected]