Partager via


Fonctions JScript définies par l'utilisateur

Même si JScript comprend de nombreuses fonctions intégrées, vous pouvez créer vos propres fonctions. La définition d'une fonction comprend une instruction de fonction et un bloc d'instructions JScript.

Définition de votre propre fonction

La fonction checkTriplet de l'exemple suivant accepte comme arguments les longueurs des côtés d'un triangle. Elle calcule si le triangle est rectangle en contrôlant si les trois nombres vérifient le théorème de Pythagore (le carré de la longueur de l'hypoténuse d'un triangle rectangle est égal à la somme des carrés des longueurs des deux autres côtés). La fonction checkTriplet appelle l'une de deux autres fonctions pour effectuer le test.

Notez l'utilisation d'un tout petit nombre (epsilon) en guise de variable de test dans la version à virgule flottante du test. En raison des incertitudes et des erreurs d'arrondi dans les calculs à virgule flottante, il n'est pas pratique d'effectuer un test direct pour contrôler si les trois nombres vérifient le théorème de Pythagore sauf dans le cas où ces trois valeurs sont des entiers. Un test direct étant plus précis, le code de cet exemple détermine si cela est approprié, et dans l'affirmative, l'utilise.

L'annotation de type n'est pas utilisée lorsque vous définissez ces fonctions. Pour cette application, il est utile que la fonction checkTriplet accepte les types de données entier et à virgule flottante.

const epsilon = 0.00000000001; // Some very small number to test against.

// Type annotate the function parameters and return type.
function integerCheck(a : int, b : int, c : int) : boolean {
   // The test function for integers.
   // Return true if a Pythagorean triplet.
   return ( ((a*a) + (b*b)) == (c*c) );
} // End of the integer checking function.

function floatCheck(a : double, b : double, c : double) : boolean {
   // The test function for floating-point numbers.
   // delta should be zero for a Pythagorean triplet.
   var delta = Math.abs( ((a*a) + (b*b) - (c*c)) * 100 / (c*c));
   // Return true if a Pythagorean triplet (if delta is small enough).
   return (delta < epsilon);
} // End of the floating-poing check function.

// Type annotation is not used for parameters here. This allows 
// the function to accept both integer and floating-point values 
// without coercing either type.
function checkTriplet(a, b, c) : boolean { 
   // The main triplet checker function.
   // First, move the longest side to position c.
   var d = 0; // Create a temporary variable for swapping values
   if (b > c) { // Swap b and c.
      d = c;
      c = b;
      b = d;
   }
   if (a > c) { // Swap a and c.
      d = c;
      c = a;
      a = d;
   }

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

// Test the function with several triplets and print the results.
// Call with a Pythagorean triplet of integers.
print(checkTriplet(3,4,5));
// Call with a Pythagorean triplet of floating-point numbers.
print(checkTriplet(5.0,Math.sqrt(50.0),5.0));
// Call with three integers that do not form a Pythagorean triplet.
print(checkTriplet(5,5,5));

Le résultat généré par ce programme est le suivant :

true
true
false

Voir aussi

Référence

function, instruction

Autres ressources

Fonctions JScript

Types de données JScript