Thursday, September 21, 2023

Functions In Typescript Part 10

Functions is a method that defines specific action for an object.

Functions are building blocks in an application to define functionalities.

Functions are defined for handling code reusability and maintability.

The process of extracting a set of statements into the function is known as refraction.

Syntax:

function identifier(params)--> Formal Params

{

definition

}

function call;

identiifer(params);


Parameter Less Function:

A function signature can be defined without parameters so that you can access the function to reuse the set of statements defined.

A parameterless function handle specific functionality every time that returns same set of output.

Syntax:

function Hello()

{

Console.log("Welcome")

}

Hello();


Parameterized Function:

Syntax

function Hello(msg:string)

{

console.log(msg);

}

Hello("typescript");

Hello("angular");

The parameter that are defined in function signature are known as formal parameters.

The parameters values that are passed in function call are known as actual parameters.


Optional Parameters

Every function defined in a function is a mandatory, i.e you have to assign a value in a function call, you can configure optional parameters by using  null reference character.

*optional parameter can be declared in 2 ways

 1.using initialization
   syntax:
      <methodname>(para1,para2=value)
      { .. }                 |
                          optional

 2.using ? symbol
    syntax:
       <methodname>(para1,para2?:<type>)
        { .. }              |
                          optional 

Ex:

function printName(fname: string, lname ?:string)

{

if(!lname)

{

console.log("fname + " " +lname);

}

else

{

console.log(fname);

}

}


Example:


requirement:
------------
          emp class
             |
          empno
          ename----non static fields
          sal
          count---static field
 
          constructor(--){}
          incrsal(amt=2000){} --method with optional para
          display(){}
          getempscount()     --static method
          { return count }




Output



Array Parameters: A  function can be defined a single parameter that can handle multiple value. It is an Array Any Type Parameters.

The value into a function can be passed dynamically allocating array type memory.

Example:

function PrintList(list :string[])

{

for(var i=0;i<list.length;i++) {

console.log(list[i]); }

}

let name:string[] =["John", "David"];

PrintList(name);

PrintList(new Array("Delhi", "Hyd"));


Generic Type Function

The term generic represent type safe.

Generic Type is Type Safe, but handles strongly typed values.

The value type will be determined dynamically.

Syntax:

function Identifier<Type> (param:T) {

//statements

}

The generic type variables cannot be used over any type of operators as data type is not determined.

Example 

function Sum(a, b)

{

return a+b;

}

function Print<T>(a:T, b: T)

{

console.log("Addition= " + sum(a,b));

}

Print<number>(10,30)


Function Default Parameters

A function can be defined with parameters specified with default values they are automatically designated as optional parameters.

The default parameters can access the value initialized and use in the function call.

You can override the default parameter values.

function Print(id:number,name:string,price:number=4500.56) {

console.log("id="  + id + "\n " + "Name= " +name +  "\n" + "Price= " +price);

}

Print(1,"TV");

Print(2,"Mobile",1200);


Anonyonous Function

It is mostly used in the Ajax Call back functions.

Is a method without identifier.

It is mostly used in the function closure for callback.

A callback function automatically executes according to the Promise.

You can dynamically create a function and load into memory reference so that you can access by the reference name.

Example

var obj=function(msg)  {

console.log(msg);

}

obj("Welcome");

An anonymous function can also include parameter types and return type.

Example
let Sum = function(x: number, y: number) : number
{
    return x + y;
}

Sum(2,3); // returns 5


Function Recurssion

It is the process of executing the function within the function definition.

Recursion is used to handle batch Jobs.

A batch job continously executes set of statements until it encounter termination criteria.

Example

function fact(n:number)

if(n<=0) {

return 1;

}

else 

{

return (n*(fact(n-1)));

}

console.log(fact(5));


TypeScript - Arrow Functions

They are used for anonymous functions i.e for function expressions. They are also called lambda functions in other languages.



Using fat arrow =>, we dropped the need to use the function keyword. Parameters are passed in the parenthesis (), and the function expression is enclosed within the curly brackets { }.

let sum = (x: number, y: number): number => {
    return x + y;
}

sum(11, 20);


No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...