Starting Out with Programming Logic and Design
CHAPTER
6 Functions
TOPICS
6.1 Introduction to Functions: Generating Random Numbers
6.2 Writing Your Own Functions 6.3 More Library Functions
6.1Introduction to Functions: Generating Random Numbers
CO NCEP T:
A function is a module that returns a value back to the part of the program that called it. Most programming languages provide a library of prewritten functions that perform commonly needed tasks. In these libraries you typically find a function that generates random numbers.
In Chapter 3 you learned that a module is a group of statements that exist within a program for the purpose of performing a specific task. When you need the module to perform its task, you call the module. This causes the program to execute the statement inside the module.
A function is a special type of module. It is like a regular module in the following ways:
l A function is a group of statements that perform a specific task. l When you want to execute a function, you call it.
When a function finishes, however, it returns a value back to the statement that called it. The value that is returned from a function can be used like any other value: it can be assigned to a variable, displayed on the screen, used in a mathematical expression (if it is a number), and so on.
227
228 Chapter 6 Functions
NOTE: It should be mentioned that functions are called differently than modules. In our pseudocode, you use the Call statement to call a module. You do not use the Call statement to call a function, however. Function calls are usually inserted into statements that perform some operation with the value that is returned from the function. For example, a function call might be inserted into a Set statement that assigns the value that is returned from the function to a variable. A function call might also be inserted into a Display statement that displays the value that is returned from the function. You will see many such examples in this chapter.
Library Functions
Most programming languages come with a library of functions that have already been written. These functions, known as library functions, are built into the programming language, and you can call them any time you need them. Library functions make a programmer's job easier because they perform many of the tasks that programmers commonly need to perform. As you will see in this chapter, there are library functions to manipulate numbers and perform various math operations, to convert data from one type to another, to manipulate strings, and more.
The code for a language's library functions is usually stored in special files. These files are normally placed on your computer when you install a compiler or interpreter. When you call a library function in one of your programs, the compiler or interpreter automatically causes the function to execute, without requiring the function's code to appear in your program. This way, you never have to see the code for a library function-- you only need to know the purpose of the library function, the arguments that you must pass to it, and what type of data it returns.
Because you do not see the internal workings of library functions, many programmers think of them as black boxes. The term black box is used to describe any mechanism that accepts input, performs some operation that cannot be seen on the input, and produces output. Figure 6-1 illustrates this idea.
Figure 6-1 A library function viewed as a black box
Input
Library Function
Output
This section demonstrates how functions work by looking at a library function that generates random numbers. Most programming languages provide such a function, and we will look at some interesting programs that can be written with it. In the next section you will learn to write your own functions. The last section in this chapter comes back to the topic of library functions and looks at several other useful functions that programming languages commonly provide.
6.1 Introduction to Functions: Generating Random Numbers 229
Using the random Function
Most programming languages provide a library function that generates random numbers. This chapter uses the random function for this purpose in the pseudocode. Random numbers are useful for lots of different programming tasks. The following are just a few examples:
l Random numbers are commonly used in games. For example, computer games that let the player roll dice use random numbers to represent the values of the dice. Programs that show cards being drawn from a shuffled deck use random numbers to represent the face values of the cards.
l Random numbers are useful in simulation programs. In some simulations, the computer must randomly decide how a person, animal, insect, or other living being will behave. Formulas can be constructed in which a random number is used to determine various actions and events that take place in the program.
l Random numbers are useful in statistical programs that must randomly select data for analysis.
l Random numbers are commonly used in computer security to encrypt sensitive data.
The following pseudocode statement shows an example of how you might call the random function. Assume that number is an Integer variable.
Set number = random(1, 100)
The part of the statement that reads random(1, 100) is a call to the random function. Notice that two arguments appear inside the parentheses: 1 and 100. These arguments tell the function to give a random number in the range of 1 through 100. Figure 6-2 illustrates this part of the statement.
Notice that the call to the random function appears on the right side of an = operator. When the function is called, it will generate a random number in the range of 1 through 100 and then return that number. The number that is returned will be assigned to the number variable, as shown in Figure 6-3.
Figure 6-2 A statement that calls the random function
Arguments Set number = random(1, 100)
Function call
Figure 6-3 The random function returns a value
Some numb
Set number = random(1, 100)
A random number in the range of 1 through 100 will be assigned to
the number variable.
er
Program 6-1 shows the pseudocode for a complete program that uses the random function. The statement in line 2 generates a random number in the range of 1 through 10 and assigns it to the number variable. (The program output shows that the number 7 was generated, but this value is arbitrary. If this were an actual program, it could display any number between 1 and 10.)
230 Chapter 6 Functions
Program 6-1
1 Declare Integer number 2 Set number = random(1, 10) 3 Display number
Program Output 7
NO TE: The way that you set up a program to work with library functions differs among programming languages. In some languages you don't have to do anything special to call library functions. That's the approach we take in our pseudocode. In other languages, however, you may have to write a statement near the top of a program indicating that it will access a particular library function.
The pseudocode in Program 6-2 shows another example. This program uses a For loop that iterates five times. Inside the loop, the statement in line 9 calls the random function to generate a random number in the range of 1 through 100.
Program 6-2
1 // Declare variables
2 Declare Integer number, counter
3
4 // The following loop displays
5 // five random numbers.
6 For counter = 1 To 5
7
// Get a random number in the range of
8
// 1 through 100 and assign it to number.
9
Set number = random(1, 100)
10
11
// Display the number.
12
Display number
13 End For
Program Output
89 7 16 41 12
The pseudocode in both Programs 6-1 and 6-2 calls the random function and assigns its return value to the number variable. If you just want to display a random number, it is not necessary to assign the random number to a variable. You can send the random function's return value directly to the Display statement, as shown here:
Display random(1, 10)
6.1 Introduction to Functions: Generating Random Numbers 231
When this statement executes, the random function is called. The function generates a random number in the range of 1 through 10. That value is returned and then sent to the Display statement. As a result, a random number in the range of 1 through 10 will be displayed. Figure 6-4 illustrates this. The pseudocode in Program 6-3 shows how you could simplify Program 6-2. This program also displays five random numbers, but this program does not use a variable to hold those numbers. The random function's return value is sent directly to the Display statement in line 4.
Figure 6-4 Displaying a random number
ome num
Display random(1, 10)
A random number in the range of 1 through 10 will be displayed.
S
ber
Program 6-3
1 // Counter variable
2 Declare Integer counter
3
4 // This loop displays five random numbers.
5 For counter = 1 To 5
6
Display random(1, 100)
7 End For
Program Output
32 79 6 12 98
In the Spotlight:
Using Random Numbers
Dr. Kimura teaches an introductory statistics class, and has asked you to write a program that he can use in class to simulate the rolling of dice. The program should randomly generate two numbers in the range of 1 through 6 and display them. In your interview with Dr. Kimura, you learn that he would like to use the program to simulate several rolls of the dice, one after the other. You decide to write a loop that simulates one roll of the dice, and then asks the user whether another roll should be performed. As long as the user answers "y" for yes, the loop will repeat. Program 6-4 shows the pseudocode for the program, and Figure 6-5 shows the flowchart.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- random selection of bituminous mixture samples
- starting out with programming logic and design
- chapter 3 discrete random variables and probability
- random numbers cpp
- hundreds chart 1 1000
- random number generation
- scanned by camscanner
- simple random sampling
- tutorial random number generation
- appendix b random numbers table and instructions
Related searches
- aristotle logic and reason
- logic and proofs explained
- logic and argument in philosophy
- starting over with no money
- starting sentences with so
- logic and philosophy answers pdf
- logic and reason philosophy
- starting sentence with but
- study of logic and reasoning
- logic and reasoning pdf
- journal of logic and analysis
- logic and reasoning in geometry