JavaScript Tutorial/Function/Function Return

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

return

The return keyword will exit the existing function and return a value.



<html>
    <body>
    <script language="JavaScript">
    <!--
    function getValue(){
         var myValue = 4*3;
         return myValue;
    }
    function fill(){
         var x = getValue();
         document.form1.tmp.value = x;
    }
    -->
    </script>
    <form name="form1">
    Value: <input type="text" Name="tmp" Size=5>
    <br>
    <br>
    <input type="button" name="get" value="Get Returned Value" onClick="fill()">
    <br>
    <br>
    </form>
    </body>
    </html>


Return an array from a function

<HTML>
   <HEAD>
   <TITLE>
   Iteration Two
   </TITLE>
   <SCRIPT>
   function makeArray() {
        var myArray = new Array(4);
        myArray[0] = "A";
        myArray[1] = "B";
        myArray[2] = "C";
        myArray[3] = "D";
        return myArray;
   }
   function showArray(theArray){
        var quote = "";
        for (var i = 0; i < theArray.length; i++){
            quote += theArray[i] + " ";
        }
        return quote;    
   }
   </SCRIPT>
   </HEAD>
   <BODY>
   <H1>
   <SCRIPT>
      var x = makeArray();
      document.write(showArray(x));
   </SCRIPT>
   </H1>
   </BODY>
</HTML>


Return boolean value from function

<html>
<head>
<script Language="Javascript" type = "text/javascript">
<!--
var newurl
function CheckRequest(newurl)
{
    if (confirm("Do you want to visit " + newurl + " site.")) {
      return true;
    } else {
      return false;
    }
}
//-->
</script>
<title>Capturing Links</title>
</head>
 <P><A href="http://www.wbex.ru" onClick = "return CheckRequest("wbex")">Java</A></P>
 <P><A href="http://www.netscape.ru" onClick = "return CheckRequest("Netscape")">Netscape</A></P>
</body>
</html>


Returning Values

JavaScript provides a return statement to return a value.

The value to be returned is placed after the keyword return.

The "undefined" value is returned from a function if no value is specified after the keyword return or if the return statement is not used.

A value returned from a function can be assigned to a variable or used within an expression.



<html>
<SCRIPT LANGUAGE="JavaScript">
    <!--
    function multiplyByFive(aNumber)
    {
      return aNumber*5;
    }
    document.write("3*5=",multiplyByFive(3));
    //-->
</SCRIPT>
</html>


Return value from a function

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function calcAvg()
{
    var inpNum1 = 1;
    var inpNum2 = 2;
    var inpNum3 = 3;
    var inpNum4 = 4;
    numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4);
    document.write("The average of the four numbers you entered is: " + numAvg);
}
function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4)
{
    var ans;
    ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4;
    return (ans);
}
//  -->
</script>
</head>
<body onLoad="calcAvg();">
</body>
</html>