<< Chapter < Page Chapter >> Page >
  • The function keyword
  • The function name: getHalf
  • An argument list containing a single parameter named incomingParameter
  • A function body enclosed in a pair of matching curly brackets

Simpler than the surface area of a sphere

The function named getHalf is somewhat simpler than one that could be used to calculate the surface area of a sphere, but the basic conceptis the same. This function expects to receive one parameter.

The code in the body of the function uses the division operator, "/", to divide the value of the incoming parameter by 2. Then it returns the result ofthat calculation. When the function returns, the return value will replace the call to the function in the calling script.

Two calls to the function

The function is called twice in the body of the script in Listing 2 , passing a different value for the parameter during each call.

Each call to the function named getHalf is embedded as one of the elements in theargument list following document.write .

write is also a function

Although I didn't mention this earlier because you weren't ready for it yet, write is also the name of a function. However, the write function is predefined in JavaScript and we can use it to display information in the browser window without having to defineit first.

(Actually, write is a special kind of a function that we call a method , but you don't need to worry about that unless you want to dig much deeper into theobject-oriented aspects of JavaScript.)

Two calls to the getHalf function

The script in Listing 2 calls the function named getHalf twice in two different locations, each of which is embedded in the argument listof a call to the write method. Each call passes a different parameter value to the getHalf function.

The order of operations

When a call to a function or method (such as the call to the write method) includes a call to another function or method in its argument list, thecall to the function in the argument list must be completed before the call can be made to the function having the argument list. Therefore in each of these twocases, the call to the getHalf function must be completed before the call to the write method can be executed.

Output from script in Listing 2

Each call to the getHalf function returns a value that is half the value of its incoming parameter.

As I mentioned earlier, when the function returns, the returned value replaces the call to the function. Therefore, ineach case, the returned value from the call to the getHalf function becomes part of the argument list for the write method before that method is called. This causes the two calls to the write method in Listing 2 to display the values returned from the calls to the getHalf function as shown in Figure 2 .

Figure 2 . Output from script in Listing 2.
Call getHalf for 10.6 Half is: 5.3Call getHalf again for 12.3 Half is: 6.15

Good programming design

It is a principle of good programming design that each function should perform only one task, and should perform it well. The task performed by thefunction named getHalf is to calculate and return half the value that it receives, and it does that task very well.

Questions & Answers

what is math number
Tric Reply
x-2y+3z=-3 2x-y+z=7 -x+3y-z=6
Sidiki Reply
Need help solving this problem (2/7)^-2
Simone Reply
x+2y-z=7
Sidiki
what is the coefficient of -4×
Mehri Reply
-1
Shedrak
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
12, 17, 22.... 25th term
Alexandra Reply
12, 17, 22.... 25th term
Akash
College algebra is really hard?
Shirleen Reply
Absolutely, for me. My problems with math started in First grade...involving a nun Sister Anastasia, bad vision, talking & getting expelled from Catholic school. When it comes to math I just can't focus and all I can hear is our family silverware banging and clanging on the pink Formica table.
Carole
I'm 13 and I understand it great
AJ
I am 1 year old but I can do it! 1+1=2 proof very hard for me though.
Atone
hi
Adu
Not really they are just easy concepts which can be understood if you have great basics. I am 14 I understood them easily.
Vedant
find the 15th term of the geometric sequince whose first is 18 and last term of 387
Jerwin Reply
I know this work
salma
The given of f(x=x-2. then what is the value of this f(3) 5f(x+1)
virgelyn Reply
hmm well what is the answer
Abhi
If f(x) = x-2 then, f(3) when 5f(x+1) 5((3-2)+1) 5(1+1) 5(2) 10
Augustine
how do they get the third part x = (32)5/4
kinnecy Reply
make 5/4 into a mixed number, make that a decimal, and then multiply 32 by the decimal 5/4 turns out to be
AJ
how
Sheref
can someone help me with some logarithmic and exponential equations.
Jeffrey Reply
sure. what is your question?
ninjadapaul
20/(×-6^2)
Salomon
okay, so you have 6 raised to the power of 2. what is that part of your answer
ninjadapaul
I don't understand what the A with approx sign and the boxed x mean
ninjadapaul
it think it's written 20/(X-6)^2 so it's 20 divided by X-6 squared
Salomon
I'm not sure why it wrote it the other way
Salomon
I got X =-6
Salomon
ok. so take the square root of both sides, now you have plus or minus the square root of 20= x-6
ninjadapaul
oops. ignore that.
ninjadapaul
so you not have an equal sign anywhere in the original equation?
ninjadapaul
hmm
Abhi
is it a question of log
Abhi
🤔.
Abhi
I rally confuse this number And equations too I need exactly help
salma
But this is not salma it's Faiza live in lousvile Ky I garbage this so I am going collage with JCTC that the of the collage thank you my friends
salma
Commplementary angles
Idrissa Reply
hello
Sherica
im all ears I need to learn
Sherica
right! what he said ⤴⤴⤴
Tamia
hii
Uday
hi
salma
hi
Ayuba
Hello
opoku
hi
Ali
greetings from Iran
Ali
salut. from Algeria
Bach
hi
Nharnhar
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?

Ask