please dont rip this site
Microsoft® JScript™
JScript Functions
 JScript Tutorial 
 Previous | Next 


What Is a Function?
Microsoft JScript functions perform actions. They can also return results. Sometimes these are the results of calculations or comparisons.

Functions combine several operations under one name. This lets you streamline your code. You can write out a set of statements, 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 the information 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.

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

Special Built-in Functions
The JScript language includes several built-in functions. Some of them let you handle expressions and special characters, and convert strings to numeric values.

For example, 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.

Another 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.
Consult the language reference for more information about these and other built-in functions.

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 three 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.

© 1997 by Microsoft Corporation. All rights reserved.


file: /Techref/language/jscript/js9.htm, 6KB, , updated: 1997/9/30 02:45, local time: 2024/11/16 03:27,
TOP NEW HELP FIND: 
13.59.163.1: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://linistepper.com/techref/language/jscript/js9.htm"> JScript Functions</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?