function myFunction(myvar1, myvar2) {
// the function's code
sum = myvar1 + myvar2;
document.write(sum);
return sum;
}
answer = myFunction(10, 5);
answer
in the example above will catch the
object returned by myFunction (which in this case will be the number
15). Note the keyword return
in the function. Return
means: leave the function now, returning this object. You can have
several returns in a function. E.g:
// Convert numbers into words
function anotherFunction(myvar) {
if (myvar == 1) {
return "One";
}
if (myvar == 2) {
return "Two";
}
return "I can't count that high";
// Any code below here would never be run.
}
// This function takes an array as input, and returns a randomly chosen
element.
function pickRandom(myarray) {
x=Math.floor( Math.random() * myarray.length);
return myarray[x];
}
// Create an array of quotes about computing
a=["To err is human, but to really foul things up you need a computer.
(Paul Ehrlich)",
"Just remember: you're not a 'dummy', no matter what those computer
books claim. The real dummies are the people who, though technically
expert, couldn't design hardware and software that's usable by normal
consumers if their lives depended upon it. (Walter Mossberg)",
"For a long time it puzzled me how something so expensive, so leading
edge, could be so useless, and then it occurred to me that a computer
is a stupid machine with the ability to do incredibly smart things,
while computer programmers are smart people with the ability to do
incredibly stupid things. They are, in short, a perfect match. (Bill
Bryson)",
"Computer Science: 1. A study akin to numerology and astrology, but
lacking the precision of the former and the success of the latter. 2.
The boring art of coping with a large number of trivialities. (Stan
Kelly-Bootle)",
"Man is the best computer we can put aboard a spacecraft...and the only
one that can be mass produced with unskilled labor. (Wernher von
Braun)",
"If there's one thing that computers do well, it's to make the same
mistake uncountable times at inhuman speed. (Peter Coffee)"];
// Pick one
quote = pickRandom(a);
// Display it
document.write(quote);
function getDateString() {
now = new Date();
bits = now.toString().split(" ");
// Arrays, which we use to convert numbers from the
date object into strings
days = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"];
months =
["January","February","March","April","May","June","July","August","September","October","November","December"];
mystring = days[now.getDay()-1] + ", " +
now.getDate() + " " + months[now.getMonth()];
return mystring;
}
document.write( getDateString() );
Warning: There are a couple of eccentricities in how Date works. The days run from 1 to 7 (i.e. they don't start at 0). However the months run from 0 to 11. The year starts from 1900 (so this year is 105) - just add 1900 to get the correct year.
setTimeout("alert('Hello')", 5000);
By repeatedly calling setTimeout(), we can make continuing updates or animations. The example below shows how to write a javascript clock:
First create a display for the clock. We use a dummy form with a text element that we can get hold of by id.
<form>
<input id="Clock" type="text" size="8" >
</form>
<script>
// Now define a function to update the clock
function setClock() {
theDate = new Date();
theTime = theDate.getHours() + ":" + theDate.getMinutes() + ":" + theDate.getSeconds();
document.getElementById("Clock").value = theTime;
// By calling setTimeout again, this function will run every 1/2 a second.
setTimeout("setClock()",500);
}
// Now start the clock going...
setClock();
</script>
In the clock example above, we get round this by using a 'dummy' form with a text element. By setting the value of the text element, we can display changing text to the viewer.
<script src="myJavaScriptFunctions.js"></script>