Reader Level:
ARTICLE

Function With Return Type in TypeScript

On December 10, 2012 Articles | TypeScript
In this article I explain what a return type function is and how to convert temperature in TypeScript.
  • 0
  • 0
  • 4964

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

class Parameter_function

{

   temperature(temp_c:number):number

   {

       var temp_f;

       temp_f = (1.8 * temp_c) + 32;

       return temp_f;

    }

}

window.onload = () =>

{

    var greeter = new Parameter_function();

    var temp_c,temp_f;

    temp_c = parseFloat(prompt("Enter the value of Temperature in Celsius: "));

    temp_f=greeter.temperature(temp_c);

     var span = document.createElement("span");

     span.innerText ="Converted Fahrenheit value is -> "+ temp_f;

     document.body.appendChild(span); 

};

 

 

demo.htm

<!DOCTYPEhtml>

 

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

<head>

    <metacharset="utf-8"/>

    <title>Function With Return Type</title>

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

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

</head>

<body>

    <h3>Function With Parameter and Return Type Example in TypeScript</h3>

    <h3>Convert Celsius to Fahrenheit</h3>

    <divid="content"/>

</body>

</html>

 

app.js

var Parameter_function = (function () {

    function Parameter_function() { }

    Parameter_function.prototype.temperature = function (temp_c) {

        var temp_f;

        temp_f = (1.8 * temp_c) + 32;

        return temp_f;

    };

    return Parameter_function;

})();

window.onload = function () {

    var greeter = new Parameter_function();

    var temp_c;

    var temp_f;

 

    temp_c = parseFloat(prompt("Enter the value of Temperature in Celsius: "));

    temp_f = greeter.temperature(temp_c);

    var span = document.createElement("span");

    span.innerText = "Converted Fahrenheit value is -> " + temp_f;

    document.body.appendChild(span);

};

 

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/

Nitin Bhardwaj

Normal 0 false false false EN-US X-NONE X-NONE ...

  • 50rank
  • 1M readers
COMMENT USING

Trending up