please dont rip this site
Microsoft® JScript
Functions
| Tutorial |


What Is a Function? Functions combine several operations under one name. This lets you streamline your code, because you can write out a set of statements just once, name it, and then execute the entire set any time you want to, just by calling it and passing to it any information it needs. You pass information to a function by enclosing it in parentheses after the name of the function. Pieces of information that are being passed to a function are called arguments or parameters. Some functions don't take any arguments at all; some functions take one argument; some take several. There are even functions for which the number of arguments depends on how you are using the function.

Microsoft JScript supports two kinds of functions: those that are built into the language, and those you create yourself.

A function that is part of an object is called a method. There are many methods built into JScript; some of these are properties of the Math, Date, and string objects, and are discussed in detail in the language reference.

Returning Values from Functions JScript functions perform actions. They can also return results. Sometimes these are the results of calculations or comparisons, as in the following example.


function isItTheMillenniumYet()  {  //  The function definition begins here.
var nowadays = new Date();
nowadays = nowadays.getYear() + 1900;
    if (nowadays >= 2001) {  //  Calculation, based on the "end of 2000" definition of millennium.
        return nowadays+"True";
    }
    else return nowadays+"False";
}  //  End of the function definition.

var newCycle = isItTheMillenniumYet();  //  The function is called here.
var theYear = newCycle.substring(0,4);  //  Capture the year from the beginning of the return.
newCycle = newCycle.substring(4,newCycle.length);  //  Capture "True" or "False".
var comment1 = "Nowadays, it's " + theYear;  //  Construct the first part of the response.
var query1 = "The Millennium Has Arrived. True or false?"; // Construct the second part.
var comment2 = newCycle + "... and there you have it."; //  Construct the final part.

Special Built-in Functions Built-in functions are part of the JScript language. Some of the built-in functions let you handle expressions and special characters, and convert strings to numeric values.


escape(aString)
unescape(aString)
eval(aString)
parseFloat(aString)
parseInt(aString,anOptionalRadix)
The first two of these, escape() and unescape(), are used to convert characters that have special meanings in HTML code, characters that you cannot just put directly into text. For example, the angle brackets, "<" and ">", delineate HTML tags.

The escape function takes as its argument any of these special characters, and returns the escape code for the character. Each escape code consists of a percent sign (%) followed by a two-digit number. The unescape function is the exact inverse. It takes as its argument a string consisting of a percent sign and a two-digit number, and returns a character. Please consult the language reference for more information about these and other built-in functions.

One extremely useful built-in function is eval(), which evaluates any valid mathematical expression that is presented in string form. The eval() function takes one argument, the expression to be evaluated.


var anExpression = "6 * 9 % 7";
var total = eval(anExpression);        //  Assigns the value 5 to the variable total.
var yetAnotherExpression = "6 * (9 % 7)";
total = eval(yetAnotherExpression)        //  Assigns the value 12 to the variable total.

var totality = eval("...surrounded by acres of clams.");        // Generates an error.

Creating Your Own Functions You can create your own functions and use them where you need them. A function definition consists of a function statement and a block of JScript statements.

The checkTriplet function in the following example takes as its arguments the lengths of the sides of a triangle, and calculates from them whether the triangle is a right triangle by checking whether the 3 numbers constitute a Pythagorean triplet. (The square of the length of the hypotenuse of a right triangle is equal to the sum of the squares of the lengths of the other two sides.) The checkTriplet function calls one of two other functions to make the actual test.

Notice the use of a very small number ("epsilon") as a testing variable in the floating-point version of the test. Because of uncertainties and roundoff errors in floating-point calculations, it is not practical to make a direct test of whether the square of the hypotenuse is equal to the sum of the squares of the other two sides unless all three values in question are known to be integers. Because a direct test is more accurate, the code in this example determines whether it is appropriate and, if it is, uses it.


var epsilon = 0.0000000000001;  //  Some very small number to test against.
var triplet = false;

function integerCheck(a, b, c)  {  //  The test function for integers.
    if ( (a*a) == ((b*b) + (c*c)) )  {  //  The test itself.
    triplet = true;
    }
}  //  End of the integer checking function.

function floatCheck(a, b, c)  {  //  The test function for floating-point numbers.
var theCheck = ((a*a) - ((b*b) + (c*c)))  //  Make the test number.
    if (theCheck < 0)  {  //  The test requires the absolute value, so invert theCheck if it's negative.
    theCheck *= -1;
    }
    if (epsilon > theCheck)  {  //  If it's as close as that, it's pretty darn close!
    triplet = true;
    }
}  //  End of the floating-poing check function.


function checkTriplet(a, b, c)  {  //  The triplet checker. First, move the longest side to position "a".
var d = 0;  //  Create a temporary holding bin.
    if (c > b)  {  // If c > b, swap them.
    d = c;
    c = b;
    b = d;
    }  //  If not, ignore them.
    if (b > a)  {  // If b > a, swap them.
    d = b;
    b = a;
    a = d;
    }  //  If not, ignore them.

//  Side "a" is now the hypotenuse, if there is one.

    if (((a%1) == 0) && ((b%1) == 0) && ((c%1) == 0))  {  //  Test all 3 values. Are they integers?
    integerCheck(a, b, c);  //  If so, use the precise check.
    }
    else
        floatCheck(a, b, c);  //  If not, get as close as is reasonably possible.
}  //  End of the triplet check function.


//  The next three statements assign sample values for testing purposes.
var sideA = 5;
var sideB = 5;
var sideC = Math.sqrt(50);

checkTriplet(sideA, sideB, sideC);  //  Call the function. After the call, triplet contains the result.

© 1996 by Microsoft Corporation.


file: /Techref/language/asp/js/307.htm, 7KB, , updated: 1996/11/22 11:12, local time: 2024/3/29 06:44,
TOP NEW HELP FIND: 
18.232.179.191:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/asp/js/307.htm"> Microsoft&#174; JScript Language Tutorial </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .