Parameterized Function – Part 2

Let’s continue Part 1 of Parameterized Functions.

Recursion Functions
  • Recursion functions are those functions that call themselves repeatedly using different parameters.
  • They generally call themselves repeatedly using loops until they reach a certain break-point/result.
    Below is an example. This is a very famous example of recursion.
var n:number = 8;
console.log("Factorial of " + n + " is " + factorial(n));
function factorial(num:number){
    if(num<=0){
        return 1;
    }
    else{
        return (num * factorial(num-1));
    }
}

Below is the output of above code:

Anonymous Functions
  • These functions do not have name and cannot be reused.
  • They function as normal functions like taking parameters and returning value.
    Below is an example:
var sum=function(num1:number, num2:number){
    return (num1+num2);
}
console.log("Sum is: " + sum(10,20));

Below is the output of above code. You can also print the value inside the function as well.

Lambda Functions
  • They are like anonymous functions in TypeScript. They are called as Arrow Functions.
  • We can pass parameters in these function and also write function’s statements in this, the way we wrote for other functions.
  • The only difference is the Lambda Notation(=>).
  • We generally use a single character to declare function parameters.

Lambda Expression
It is a one liner code which acts as a function.

Syntax for Lambda Expression:

(parameter1, parameter2,..) => function statements

Below is an example:

var n2:number = 20;
var sum=(n1:number, n3:number) => n2 + n1 + n3;
console.log("Sum is: " + sum(10,30));

Below is the output of above code:

Lambda Statement
It points to a block of code.
Below is the syntax for Lambda Statement:

(parameter1, parameter2,..) => {
    function statements
}

Below is an example:

var n2:number = 20;
var add=(n1:number, n3:number) => {
    var sum = n2 + n1 + n3;
    console.log("Sum is: " + sum);
}
add(100,30);

Below is the output of above code:

Function Constructor
  • We can also define a function using JavaScript’s build in constructor called Function().
    It’s syntax:
var result = new Function(parameter1, parameter2,....)

Below is an example:

var add = new Function("num1", "num2", " return (num1 + num2)" );
var sum=add(10,20);
console.log("Sum is: " + sum);

Below is the output of above code:

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started