Chapter 6 Functions Review Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is a substring?

A string within a string. The substring function typically accepts three arguments: (1) a string that you want to extract a substring from, (2) the beginning position of the substring, and (3) the ending position of the substring.

This type of function returns either True or False.

Boolean function

(Short Answer) What is the difference between a module and a function?

Both Modules and functions share the two below in common: 1. A function is a group of statements that perform a specific task. 2. When you want to execute a function, you call it. The difference is, 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. 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.

In a case-sensitive comparison, the strings "yoda" and "YODA" are equivalent.

False (capital and lower case letters have different values)

(T or F) The code for a library function must appear in a program in order for the program to call the library function.

False (these are stored in the computer, but not in the code)

In many languages it is an error to assign a real number to an integer variable.

False (you can assign an integer to a real variable)

This is a design tool that describes the input, processing, and output of a function.

IPO Chart

6.6 What is the purpose of the Return statement in a function?

One of the statements in function body must be a Return statement. A Return statement specifies the value that is returned from the function when the function ends.

What is the purpose of the stringToInteger and stringToReal functions described in this chapter?

The stringToInteger function accepts a string as an argument, converts it to an Integer, and returns the Integer value. The stringToReal function works the same way as stringToInteger, but it converts a string to a Real. provide library functions that convert strings to numbers.

Complex mathematical expressions can sometimes be simplified by breaking out part of the expression and putting it in a function.

True

In some languages you must use a library function to raise a number to a power.

True (Some programming languages, however, do not have an operator that raises a number to a power. Instead, they use a function such as pow.)

6.1 How does a function differ from a module?

When a function finishes 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.

why do you never see the code for a library function?

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.

6.3 Why are library functions like "black boxes"?

any mechanism that accepts input, performs some operation that cannot be seen on the input, and produces output.

This term describes any mechanism that accepts input, performs some operation that cannot be seen on the input, and produces output.

black box

This part of a function definition is comprised of one or more statements that are executed when the function is called.

body

6.2 What is a library function?

built into the programming language, and you can call them any time you need them.

This part of a function definition specifies the data type of the value that the function returns.

header

Where is the code for library functions usually stored?

in special files normally placed on your computer when you install a compiler or interpreter.

(MT) This is a prewritten function that is built into a programming language.

library function

In pseudocode, this statement causes a function to end and sends a value back to the part of the program that called the function.

return

This is a string inside of another string.

substring

When a conversion function is used to convert a real number to an integer, what usually happens to the real number's fractional part?

the fractional part will be thrown away. Example: 2.5 will return a 2 when converted from real number to an integer.

What three characteristics of a function are described in an IPO chart?

the input, processing, and output of a program

What is the purpose of the isInteger and isReal functions described in this chapter?

the isInteger function to determine whether a string can be converted to an Integer, and the isReal function to determine whether a string can be converted to a Real. To help prevent errors when converting strings to numbers, many programming languages provide library functions that test a string and then return either True or False indicating whether the string can successfully be converted to a number.

6.7 Look at the following pseudocode function definition: Function Integer doSomething(Integer number) Return number * 2 End Function 1. What is the name of the function? 2. What type of data does the function return? 3. Given the function definition, what will the following statement display? Display doSomething (10)

the name of the function is doSomething. the data type returned is an integer. will display a 10

Who do library functions make a programmers job easier?

they perform many of the tasks that programmers commonly need to perform. Examples: manipulate numbers and perform various math operations, to convert data from one type to another, to manipulate strings, and more.

6.4 In pseudocode, what does the following statement do? Set x = random(1, 100)

to call a random number between the range of (1,100) and returned and assigned to the random variable to x.

6.5 In pseudocode, what does the following statement do? Display random(1, 20)

to display a random number between 1 and 20

Although you dont see the code for library functions, what three things do you need to know?

to know the purpose of the library function, the arguments that you must pass to it, and what type of data it returns.

This is an example of a data type conversion function in pseudocode.

toReal

This type of error occurs when you try to assign a value of one data type to a variable of another data type.

type mismatch error

6.8 What is a Boolean function?

used to test a condition, and then return either True or False to indicate whether the condition exists.


Set pelajaran terkait

NU143- Chapter 23: Nursing Care of the Newborn with Special Needs

View Set

6.3 - Financial Goals/Objectives

View Set

Prep-U Chapter 44: Assessment and management of patients with biliary disorders

View Set

"What am I?" Guess the answer to the tricky questions!

View Set