Function With Return Type in TypeScript

Introduction

 
A function is a block of code that you can use multiple times in an application. It can require one or more parameters. These are some rules of functions:
  • A function starts with "{" and ends with "}".
  • At the time of calling a function, the function signature must be the same.
  • A function can return a value using the return statement in conjunction with a value or object.
  • In the return value of a function, the return keyword is used to return a value from a function.
  • In the return value of a function, the return stops the execution of the function and sends the value back to the calling code.
Example
 
The following example shows the return type function in TypeScript. This example converts temperatures from Celsius to Fahrenheit. Let's use the following.
 
Step 1
 
Open Visual Studio 2012 and click "File" -> "New" -> "Project..". A window is shown. Provide the name of your application as "Return_function" and then click ok.
 
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 files.
 

Coding

 
parameter_function.ts
  1. class Parameter_function {  
  2.  temperature(temp_c: number): number {  
  3.   var temp_f;  
  4.   temp_f = (1.8 * temp_c) + 32;  
  5.   return temp_f;  
  6.  }  
  7. }  
  8. window.onload = () => {  
  9.  var greeter = new Parameter_function();  
  10.  var temp_c, temp_f;  
  11.  temp_c = parseFloat(prompt("Enter the value of Temperature in Celsius: "));  
  12.  temp_f = greeter.temperature(temp_c);  
  13.  var span = document.createElement("span");  
  14.  span.innerText = "Converted Fahrenheit value is -> " + temp_f;  
  15.  document.body.appendChild(span);  
  16. };  
demo.htm
  1. <!DOCTYPEhtml>  
  2. <htmllang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <metacharset="utf-8"/>  
  6.         <title>Function With 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 and Return Type Example in TypeScript</h3>  
  13.         <h3>Convert Celsius to Fahrenheit</h3>  
  14.         <divid="content"/>  
  15.     </body>  
  16. </html>  
app.js
  1. var Parameter_function = (function() {  
  2.  function Parameter_function() {}  
  3.  Parameter_function.prototype.temperature = function(temp_c) {  
  4.   var temp_f;  
  5.   temp_f = (1.8 * temp_c) + 32;  
  6.   return temp_f;  
  7.  };  
  8.  return Parameter_function;  
  9. })();  
  10. window.onload = function() {  
  11.  var greeter = new Parameter_function();  
  12.  var temp_c;  
  13.  var temp_f;  
  14.  temp_c = parseFloat(prompt("Enter the value of Temperature in Celsius: "));  
  15.  temp_f = greeter.temperature(temp_c);  
  16.  var span = document.createElement("span");  
  17.  span.innerText = "Converted Fahrenheit value is -> " + temp_f;  
  18.  document.body.appendChild(span);  
  19. };  
Output 1
 
Enter the temperature in Celsius:
 
enter-temperature.jpg
 
Output 2
 
Converted Fahrenheit value:
 
final-result-in-fahrenheit.jpg
 
Reference By
http://www.typescriptlang.org/


Similar Articles