JavaScript Tutorial/Function/Function Definition

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

Calculation in function

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function inputCels()
{
    var cels = 100;
    ansFah = doFahCalc(cels);
    alert(cels + " Degrees Celsius is " + ansFah + " Degrees Fahrenheit");
}
function inputFah()
{
    var fah = 100;
    ansCel = doCelCalc(fah);
    alert(fah + " Degrees Fahrenheit is " + ansCel + " Degrees Celsius");
}
function doCelCalc(fah)
{
    var ans = ((Number(fah) - 32) / 1.8);
    return (ans);
}
function doFahCalc(cels)
{
    var ans = ((1.8 * Number(cels)) + 32);
    return (ans);
}
//  -->
</script>
</head>
<body>
<input type="button" value="Convert Celsius to Fahrenheit" onClick="inputCels();">
<br>
<br>
<input type="button" value="Convert Fahrenheit to Celsius" onClick="inputFah();">
</body>
</html>


Define a function

<html>
<head>
<title>Function Demo</title>
<script language="javascript" type="text/javascript">
<!--
function greetVisitor()
{
     var myName = prompt("Name.", "");
     alert("Welcome " + myName + "!")
}
//-->
</script>
</head>
<body>
<h1>Function Demo</h1>
</body>
</html>


Define the simplest function

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function yourMessage()
{
    alert("Your first function!");
}
//  -->
</script>
</head>
<body>
</body>
</html>


Functions

All function declaration must begin with the keyword function followed by the name of the function.

Parentheses are placed after the function name to hold arguments.

If more than one argument are used, use commas to separate the arguments.

If no arguments, leave the space between the parentheses empty.

Curly brackets are used to contain the code related to the function.

Curly brackets are not optional.

JavaScript uses call by value.



<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
    var aString = "A"
    var aNumber = 1;
    function test(aString, aNumber) {
      aString = "B";
      aNumber = 2;
      document.write("<BR><BR><BR><BR><BR>During function call:<BR>");
      document.write("aStringCopy=",aString,"<BR>");
      document.write("aNumberCopy=",aNumber,"<BR>");
    }
    document.write("<BR><BR><BR><BR><BR>Before function call:<BR>");
    document.write("aString=",aString,"<BR>");
    document.write("aNumber=",aNumber,"<BR>");

    test(aString,aNumber);

    document.write("<BR><BR><BR><BR><BR><BR><BR>After function call:<BR>");
    document.write("aString=",aString,"<BR>");
    document.write("aNumber=",aNumber,"<BR>");
    //-->
</SCRIPT>
</html>


Recursive function

<HTML>
<HEAD>
   <SCRIPT>
   function fib (inNum) {
      if (inNum == 0) 
        var fibNum = 0;
      else{
        if (inNum == 1)
           fibNum = 1;
        else{
           fibNum = fib(inNum - 2) + fib(inNum - 1);
        }
     }
     return fibNum;
   }
   function writeFibs(topFib) {
      for (var i=0;  i <= topFib ; i++) {
        document.write ("Fib(" + i + ") = " + fib(i) + " <br>");
      }
   }
   
   </SCRIPT>
</HEAD>
<BODY>
   <FORM Name="theForm">
   <INPUT Type=Text Name="numFibs">
   <INPUT Type=Button Value="Show Fibs" onClick="writeFibs(numFibs.value);">
</FORM>
</BODY>
</HTML>