<< 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:

Questions & Answers

how do I set up the problem?
Harshika Reply
what is a solution set?
find the subring of gaussian integers?
hello, I am happy to help!
Shirley Reply
please can go further on polynomials quadratic
hi mam
I need quadratic equation link to Alpa Beta
Abdullahi Reply
find the value of 2x=32
Felix Reply
divide by 2 on each side of the equal sign to solve for x
Want to review on complex number 1.What are complex number 2.How to solve complex number problems.
yes i wantt to review
use the y -intercept and slope to sketch the graph of the equation y=6x
Only Reply
how do we prove the quadratic formular
Seidu Reply
please help me prove quadratic formula
hello, if you have a question about Algebra 2. I may be able to help. I am an Algebra 2 Teacher
Shirley Reply
thank you help me with how to prove the quadratic equation
may God blessed u for that. Please I want u to help me in sets.
what is math number
Tric Reply
x-2y+3z=-3 2x-y+z=7 -x+3y-z=6
Sidiki Reply
can you teacch how to solve that🙏
Solve for the first variable in one of the equations, then substitute the result into the other equation. Point For: (6111,4111,−411)(6111,4111,-411) Equation Form: x=6111,y=4111,z=−411x=6111,y=4111,z=-411
x=61/11 y=41/11 z=−4/11 x=61/11 y=41/11 z=-4/11
Need help solving this problem (2/7)^-2
Simone Reply
what is the coefficient of -4×
Mehri Reply
the operation * is x * y =x + y/ 1+(x × y) show if the operation is commutative if x × y is not equal to -1
Alfred Reply
An investment account was opened with an initial deposit of $9,600 and earns 7.4% interest, compounded continuously. How much will the account be worth after 15 years?
Kala Reply
lim x to infinity e^1-e^-1/log(1+x)
given eccentricity and a point find the equiation
Moses Reply
A soccer field is a rectangle 130 meters wide and 110 meters long. The coach asks players to run from one corner to the other corner diagonally across. What is that distance, to the nearest tenths place.
Kimberly Reply
Jeannette has $5 and $10 bills in her wallet. The number of fives is three more than six times the number of tens. Let t represent the number of tens. Write an expression for the number of fives.
August Reply
What is the expressiin for seven less than four times the number of nickels
Leonardo Reply
How do i figure this problem out.
how do you translate this in Algebraic Expressions
linda Reply
why surface tension is zero at critical temperature
I think if critical temperature denote high temperature then a liquid stats boils that time the water stats to evaporate so some moles of h2o to up and due to high temp the bonding break they have low density so it can be a reason
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?