In the first page of this tutorial we looked
at the smallest elements of a program, the
constants,
variables,
and operators, from which we made
expressions, and touched on assignment statements. Then we looked at statements
in general, and how to form them into larger structures. Now we will look
at the largest structures, called *functions,* which form the basis
of programs.

A ** function** is a block of statements (enclosed in required
braces) with a given name (identifier)
and a list of variables called

The parameters are essentialy variable names used within thefunctionname(param1,param2, ...) {statements}

That sounds abstract, let's look at a specific (but somewhat contrived) example. Suppose we had several pairs of numbers, and we wanted to display the sum, difference, and products of each pair on separate lines. We could tediously write:

Or we could write a simple function to do the heavy lifting, then call it for each pair:var first = 5;var second = 4;var sum = first+second;var difference = first - second;var product = first * second;document.write("<p>Sum of "+first+"+"+second+"="+sum+ ", diff ="+difference+", product ="+product);first = 3;second = 7;sum = first+second;difference = second - first;product = first * second;document.write("<p>Sum of "+first+"+"+second+"="+sum+ ", diff ="+difference+", product ="+product);first = 2;second = 2;sum = first+second;difference = first - second;product = first * second;document.write("<p>Sum of "+first+"+"+second+"="+sum+ ", diff ="+difference+", product ="+product);

Notice that the second pair reverses the difference (so it's always positive); we did that with a conditional. When you call a function, you give the name of the function, then enclose value expressions for each parameter in order, separated by commas, in parentheses, thus:function ShowSumDifProd(first,second){var sum = first+second;var difference = first - second;if (first<second) difference = second - first;var product = first * second;document.write("Sum of "+first+"+"+second+"="+sum+ ", diff ="+difference+", product ="+product);}

The function can produce a value, which is returned to the caller in the return statement:ShowSumDifProd(5,4);ShowSumDifProd(3,7);ShowSumDifProd(2,2);

If there is no value to return, but you want to return early (sort of like thereturnexpression;

Let's add one more line to our function, so it returns the quotient:return;

Calling a function that returns a value has just that value returned, which can be combined with operators and/or used anywhere else a value is permitted, for example:function ShowSumDifProd(first,second){var sum = first+second;var difference = first - second;if (first<second) difference = second - first;var product = first * second;document.write("Sum of "+first+"+"+second+"="+sum+ ", diff ="+difference+", product ="+product);return first / second;}

In this example, the function is called for (5,4), which returns 1.25; then it is called for (2,2), returning 1. Finally it is called a third time with the previous two returned values now as parameters, returning 1.25 again. What happens if the second parameter is ever zero? Try it.var quo = ShowSumDifProd(ShowSumDifProd(5,4),ShowSumDifProd(2,2));

What is the factorial of 5?function fact(n) {if (n==0) return 1; else return fact(n-1)*n;}

Recursion is a powerful software tool, but not many programmers understand it fully. The people who wrote the JavaScript are among those who do not, so there is a limit to recursion depth it allows. My browser crashed (sometimes killing the whole computer) when I tried 93 levels in this program:

Releasing to the public a program that crashes is the mark of incompetence (or choosing C to program in, which amounts to the same thing). Experienced programmers know a variety of different ways to solve programming problems. Recursion is only one of them.var tops = 92;

function recurx(deep) {if (deep==0) return;if (deep%10 ==0) document.write("<br>"+deep);else document.write(" "+deep);recurx(deep-1);if (deep%10 ==0) document.write("<br>-"+deep);else document.write(" -"+deep);}

recurx(tops);

You can also have functions inside of functions in JavaScript, but it
is not permitted in C++ nor Java, so we won't get into that.

Most of the built-in functions in JavaScript are associated with objects
(which we will look at in the next page), but a few are stand-alone functions.
One example is `isNaN(value)`, which returns **true** if the
parameter is not a number. Recall that we got "NaN" for non-numeric results
earlier. NaNs are never equal to anything (not even themselves), so discovering
you have one might be tricky, which makes this function useful.

Another useful JavaScript function is `eval(text)` which lets
you execute a text string as if it were JavaScript code. This gives the
language awesome power if you know how to use it. For the rest of us, it's
still helpful in writing confusing code, like as a way to conceal email
addresses on a web page (so they can be seen by people but hopefully not
spider bots), or encrypting text for transmission over the internet.

Next: Data Structures

Tom Pittman

Rev 2010 December 22