<< Chapter < Page Chapter >> Page >

If you did it all correctly, your program should calculate the square root. (For reasons that will become apparent later, I will refer to the code that you inserted as in-line code.)

Oops, need to do it all over again

Suppose that further on in your program you discover that you need to calculate the square root of another number. And later, you discover that youneed to calculate the square root of still another number. Obviously, with a few changes, you could copy your original code and insert it as in-line code at each location in your program where you need to calculate the square root ofa number.

Is there a better way?

However, after doing this a few times, you might start asking if there is a better way. The answer is "yes, there is a better way."

A programming module provides a better way

The better way is to create a separate program module that has the ability to calculate the square root and to make that module available for use as a helperto your main program each time your main program needs to calculate a square root. In some programming languages, this separate program module is oftencalled a function or a method. In scratch 2.0, it doesn't have a specific name. I will refer to it as a custom block . You create a custom block in Scratch 2.0 by selecting the More Blocks toolbox shown in Image A .

Image a. the more blocks toolbox.

Missing image.
Image A. The More Blocks toolbox.

Library functions

Most modern programming languages provide a large number of pre-written functions that are already available for your use.

In addition to the library functions that are already available, if you need a function to perform some operation and there is no library function alreadyavailable to perform that operation, you can write your own function.

A similar library for mathematical functions exists in Scratch 2.0. The library is accessed using the bottom block in the Operators toolbox with the selection sqrt shown in Image B .

Image b. access to scratch 2.0 math functions.

Missing image.
Image B. Access to Scratch 2.0 math functions.

Image C shows the different mathematical function selections that are available in the pull down list of the block. (Note that the fourth one from the top is the sqrt function.)

Image c. mathematical functions available in scratch 2.0.

Missing image.
Image C. Mathematical functions available in Scratch 2.0.

Passing parameters

Make the function general

Normally, in the case of designing and writing a function such as one that can calculate the square root of a number, it is desirable to write it in such away that it can calculate the square root of any number (as opposed to only one specific number) . This is accomplished through the use of something called parameters.

Pass me the number please

The process of causing a function to be executed is commonly referred to as calling the function.

When your program calls the square-root function, it will need to tell the function the value of the number for which the square root is needed. Ingeneral, many functions will require that you provide certain kinds of information when you call them. The code in the function needs this informationto be able to accomplish its purpose.

Questions & Answers

what are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
what's cornea?
Majak Reply
what are cell
Achol
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask