Function in TypeScript PART-4

In our previous post we have overview  about enum in Type Script.Today we will learn about function.

Let’s start

What is Function:

Group of statements perform together which solve a specific task. Every function should be solved a single task.There are some region why we use function like are:

  • Organization
  • Reusability
  • Testing
  • Extensibility
  • Abstraction

Function can be created both as a named function or as a anonymous function in Type Script.

Named function without parameter :

let’s get explain about code

GetFullName is function name which did not use any parameter and return type string. function and return are keyword. return keyword return value.
Output: Toufique Rahman Tshovon

Named function with parameter:

let’s get explain about code

GetFullName is function name which have taken two parameter one is firstName another one is lastName both are string type. firstName and lastName have been concatenated.

Output: Shamim uddin

Anonymous function without parameter

let’s get explain about code

There are no method name. But Name variable hold function’s perform which has no parameter.

Output: Toufique Rahman Tshovon

Anonymous function with parameter

let’s get explain about code

There are no method name. But Name variable hold function’s perform which have two parameter
One is firstName another one is lastName both are string type. firstName and lastName have been concatenated.
Default Arguments:

Output: 20

let’s get explain about code

We have assigned default argument in secondNumber. if do not pass secondNumber  then by default will be assigned 10.but if we pass secondNumber then secondNumber will be assigned like

Output: 40

Hope this will be helpful. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *