<< Chapter < Page Chapter >> Page >

Two sides to every function

There are two sides to the use of every function:

  1. The function definition.
  2. The function call.

The definition names the function and specifies how it will behave when it is called.

The call to the function temporarily passes control to the statements in the function causing them to behave as previously defined.

Once the statements have been executed, control is returned to the point in the script where the call was made. The function may, or may not return a valuewhen it returns control.

The argument list

As in the sphere example discussed above, it is often useful to pass information to the function for it to use in doing whatever it is supposed to do(but this is not always required). When we call the function, we include parameters in the call to the function that match up with the argument listmentioned above. That is the mechanism used to pass information to a function. (This will probably make more sense when you see an example. Again, in somecases, no arguments are required.)

The purpose of a function

Usually (but not always), the purpose of a function is to calculate or otherwise determine some value and return it. In the sphere example mentionedearlier, the purpose of the function would be to calculate and return the surface area of the sphere. Returning a value is accomplished using the return keyword in the body of the function.

Sometimes, the purpose of a function is not to return a value, but instead to cause some action to occur, such as displaying information in the browserwindow. In that case, a return statement is not required. However, it doesn't cause any problem to put a return statement at the end of the function's body with nothing to the right of the word return.

An example function named getHalf

The code in Listing 2 defines a function named getHalf and then calls that function from two different locations in a script, passing adifferent parameter value with each call.

Listing 2 . An example function named getHalf.
<!-- File JavaScript02.html --><html><body><script language="JavaScript1.3">//This is the syntax for a comment. //Define the function named getHalf()function getHalf(incomingParameter) { return incomingParameter/2;}//end function getHalf() document.write("Call getHalf for 10.6","</br>") document.write("Half is: ", getHalf(10.6),"</br>"); document.write("Call getHalf again for 12.3","</br>") document.write("Half is: ", getHalf(12.3));</script></body></html>

A note about comments

Note the line of code immediately following the first script tag that begins with //. Whenever JavaScript code contains such a pair of slashmarks (that are not inside of a quoted string), everything from that point to the end of the line is treated as a comment. A comment is intended for human consumptiononly and is completely ignored when the script is run.

The function definition

The function definition in Listing 1 consists of the three lines of code following the comment that begins with "//Define the function..."

As explained earlier, this function definition contains:

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask