Java Script User Defined Functions
Java Script You can define your own JavaScript functions. Such functions are called user- defined, as opposed to predefined functions which you get from a library, such as a Date() What is important to know about user-defined functions: Functions allow you to group a set of statements and give them a name. Functions are defined in the head of a document. When you define a function, the statements inside are not executed. Later you invoke the function in the body of the document, and the statements inside execute. A function may have inputs (for instance, Math.sqrt() has one input). Values of inputs are sent to the function in parentheses when the function is invoked. A function may return a value, like the promprt() and Math.random() functions.
Example Feet to Meters function displayMeters(feet) { // the function takes distance in feet // and displays the equivalent distance in meters document.write(" You entered " + feet + " feet. "); // meters is a local variable: var meters = feet * ; document.write("That's equivalent to " + meters + " meters."); } var feet = prompt("Please enter the distance measured in feet", ""); displayMeters(feet); feet= prompt("Give me one more -distance in feet-"); displayMeters(feet);
Answer these questions 1.What is the name of the function that converts the distance? 2.Where do we specify the formula for the conversion? 3.Where is the function defined, and where invoked? 4.When is it invoked? How many times? 5.What is the input (or argument or parameter) of the function? 6.How is the result displayed on the web page?
Or… Alternatively, you can write a function that computes and returns the result. In this case the function itself will not print anything in the page, the printing is done in the body of the document.
Feet to Meters function convert(feet) { // the function takes distance in feet // and returns this distance in meters return feet *0.3048; } feet = prompt("Please enter the distance measured in feet", ""); document.write(" You entered " + feet + " feet. "); document.write("That's equivalent to " + convert(feet) + " meters.");
Old MacDonald 1.Remember the Old MacDonald song? The verses of the Old MacDonald song are all the same. The only thing that changes from verse to verse, is the animal and the sound. Use the example on the next slide and paste into a new file OldMac.html and fill in the code for a JavaScript function OldMacVerse that displays one verse of the Old MacDonald song. 2.Since we invoke the function 4 times with different animals and sounds (see the example below), we get a page that displays the entire song! The resulting page should look like this.this
Check for this Old MacDonald and New Functions! function OldMacVerse(animal, sound) // Assumes: animal and sound are strings // Results: displays corresponding Old MacDonald verse { // your code goes here: } OldMacVerse("cow", "moo"); OldMacVerse("pig", "oink"); OldMacVerse("duck", "quack"); OldMacVerse("horse", "neigh");