Javascript: Functions

This entry is part 13 of 27 in the series Javascript Tutorial

Functions are blocks of code that together achieve a more complex task, and are not executed until you call upon them.

We have already made use of functions in our earlier examples.  The example below will pass 2 variables into a function.

<!DOCTYPE html>
<html>
<head>
 <title>Function</title>
 <script>
 function updateFunction(x, y) {
   var z = x + y;

   document.getElementById("int1").innerHTML = x;
   document.getElementById("int2").innerHTML = y;
   document.getElementById("int3").innerHTML = z;
 }
 </script>
</head>
<body>
 <p id="int1">Integer x</p>
 <p id="int2">Integer y</p>
 <p id="int3">Integer z</p>
 <br>
 <button onclick="updateFunction(8,10)">Click Me</button>
 </body>
</html>

button

The x, y variables are passed into the updateFunction function.  Try changing 8, 10 into different integers and run the above program again.

Series Navigation<< Javacript: Integer variables
Javascript: Operators >>