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 parentheses.
  • 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
  1. class Parameter_function {  
  2.  triangle(a: number) {  
  3.   var row: number, c: number;  
  4.   for (row = 1; row <= a; row++) {  
  5.    for (c = 1; c <= row; c++)  
  6.     document.write("*");  
  7.    document.write("<br>");  
  8.   }  
  9.  }  
  10. }  
  11. window.onload = () => {  
  12.  var greeter = new Parameter_function();  
  13.  var a: number;  
  14.  a = parseInt(prompt("Enter the number of rows in triangle of stars you wish to see"));  
  15.  greeter.triangle(a);  
  16. };  
demo.html
  1. <!DOCTYPEhtml>  
  2. <htmllang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <metacharset="utf-8"/>  
  6.         <title>Function With parameter Without Return Type</title>  
  7.         <linkrel="stylesheet"href="app.css"type="text/css"/>  
  8.         <scriptsrc="app.js">  
  9.         </script>  
  10.     </head>  
  11.     <body>  
  12.         <h3>Function With parameter Without Return Type Example In TypeScript</h3>  
  13.         <divid="content"/>  
  14.     </body>  
  15. </html>  
app.js
  1. var Parameter_function = (function() {  
  2.  function Parameter_function() {}  
  3.  Parameter_function.prototype.triangle = function(a) {  
  4.   var row;  
  5.   var c;  
  6.   for (row = 1; row <= a; row++) {  
  7.    for (c = 1; c <= row; c++) {  
  8.     document.write("*");  
  9.    }  
  10.    document.write("<br>");  
  11.   }  
  12.  };  
  13.  return Parameter_function;  
  14. })();  
  15. window.onload = function() {  
  16.  var greeter = new Parameter_function();  
  17.  var a;  
  18.  a = parseInt(prompt("Enter the number of rows in triangleof stars you wish to see"));  
  19.  greeter.triangle(a);  
  20. };  
Output 1
 
Enter-row.jpg
 
Enter the number of rows then click on the ok button.
 
Output 2
 
result-pyramid.jpg
 
Referenced By
http://www.typescriptlang.org/


Similar Articles