Function With Parameter in TypeScript

Function with Parameter in TypeScript

A Function is a block of code that you can use multiple times in an application. It can require one or more parameters. A list of parameters in parentheses, and a block of code in braces. To call a function, you code the function name followed by the function's parameters or arguments in parentheses. The following uses parameters as it executes its block of code. Some rules of functions with parameters:

  • A function must start with "{" and end with "}"
  • For calling a function simply use the function name ( Myfunction() )
  • When calling a function, the function signature must be the same.
  • In a function with a parameter, pass the parameter(s) inside parantheses.
  • You can pass as many parameters as you like. These parameters work like variables inside your function.
  • A parameter appears within parentheses "( )"

Example

The following example shows a simple function (triangle) with a parameter but no return type. In this example I have a Parameter_function class and define the triangle function. In the class we enter the number of rows in the triangle of a star. Let's use the following procedure.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown. Give the name of your application as "Prameter_function" and then click ok.


application-name.jpg


Step 2

After this session the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file, webconfig file and html file:

solution-explorer.jpg

Coding

parameter_function.ts

class Parameter_function

{

   triangle(a:number)

   {

        var row: number, c: number;

        for (row = 1; row <= a; row++)

        {

           for (c = 1; c <=row;c++)       

              document.write("*");

           document.write("<br>");

        }

    }

}

window.onload = () =>

{

    var greeter = new Parameter_function();

    var a: number;

    a = parseInt(prompt("Enter the number of rows in triangle of stars you wish to see"));

    greeter.triangle(a);

};

 

 

demo.html

<!DOCTYPEhtml>

 

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>Function With parameter Without Return Type</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h3>Function With parameter Without Return Type Example In TypeScript</h3>

    <divid="content"/>

</body>

</html>

 

app.js

var Parameter_function = (function () {

    function Parameter_function() { }

    Parameter_function.prototype.triangle= function (a) {

        var row;

        var c;

 

        for(row = 1; row <= a; row++) {

            for(c = 1; c <= row; c++) {

                document.write("*");

            }

            document.write("<br>");

        }

    };

    return Parameter_function;

})();

window.onload = function () {

    var greeter = new Parameter_function();

    var a;

    a = parseInt(prompt("Enter the number of rows in triangleof stars you wish to see"));

    greeter.triangle(a);

};

 

Output 1


Enter-row.jpg


Enter the number of rows then click on the ok button.

 

Output 2


result-pyramid.jpg

 

Reference By

http://www.typescriptlang.org/

Up Next
    Ebook Download
    View all
    Test
    Read by 16 people
    Download Now!
    Learn
    View all