Functions

Functions allow you to write lines of code that can be used again and again in different parts of a program without actually having to write the lines of code several times. You can create a function that runs some lines of code (usually a specific set of instructions) and then ‘call’ that function to be used later on. Have a look at the sample code below and make sure you watch the video here.

<!DOCTYPE html>
<html lang="en"
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Functions</title>
 <script type="text/javascript">
 
 // This is how you define a function
 function saymessage(){
 // function code goes here
 // more function code goes here
 // example of simple function code below:
 alert('Hello world');
 }
 
 // this is how you 'call' the function
 saymessage();
 
 //or you can call the function from an event like in a form, in the body section below
 </script>
 
</head>
<body>
 <form>
 <input type="button" value="click me" onclick="saymessage();"/>
 </form>
 
 
</body>
</html>