Sunteți pe pagina 1din 3

Answer Key

CPT 168
Homework #8
Chapter 3, sections 3.4-3.5
Checkpoint Questions 3.14-3.21
3.14 What are the pieces of data that are passed into a module called?
The pieces of data that are passed into a module are called arguments or actual parameters.
Example
Calling the module from the main module:
Module main()
// Declare and initialize variables
Declare Integer number1 = 0
Declare Integer number2 = 0
// Get values for number1 and number2
Display Enter the first number:
Input number1
Display Enter the second number:
Input number2
// Call the module to compute and display the result
Call Module multiplyNums(number1, number2) // number1 and number2 are
// arguments o actual
parameters.
End Module
Module multiplyNums(Integer num1, Integer num2)
Declare Integer multResult
Display The product of , num1, and , num2, is , multResult
End Module

3.15 What are the variables that receive pieces of data in a module called?
The variables that receive pieces of data in a module are called parameters (or parameter variables).
In the example above num1 and num2 are called formal parameters. When the module is called, the
value in the variable number1 in the main module will be copied into num1, and the value in the
variable number2 will be copied into num2. Number1 and Number2 are called actual parameters. So,
we pass actual parameters to formal parameters.

The arguments are passed in the order in which they are listed in the Call Module statement. So if the
Call Module statement above had instead said:
Call Module multiplyNums(number2, number1)
Then the parameter num1 would have been given the value of number2 and the parameter num2
would have been given the value of number1.

3.16 Does it usually matter whether an arguments data type is different from the data type of the
parameter that it is being passed to?
Yes, it does usually matter whether an arguments data type is different from the data type of the
parameter that it is being passed to. In general you cannot pass an argument to a parameter of a
different data type, and doing so will usually result in an error. Some languages, however, will allow
you to pass an argument into a parameter value of a different data type as long as no data will be lost.
For example, passing an argument of data type Integer to a parameter of data type Real would work
in such a language since the data type Real and hold a whole number.

3.17 Typically, what is a parameter variables scope?


A parameter variables scope is typically the entire module in which it is declared. No statement
outside the module can access the parameter.

3.18 Explain the difference between passing by value and passing by reference.
Passing an argument by value passes a copy of the arguments value to the formal parameter. The
parameter value (copy of the original value) can be manipulated and changed within the called
module, but those changes do not affect the value of the original value in the main module.
Passing an argument by reference instead passes the location of the argument (like a shortcut on
your desktop). Any actions performed on the argument passed by reference in the called module are
actually performed on the original variable, and any changes made in the called module change the
original variable itself.

3.19 What is the scope of a global variable?


The scope of a global variable (declared outside all the modules, usually at the top of the program) is
the entire program. All of the modules in the program can see, access and change a global variable.

3.20 Give one good reason that you should not use global variables in a program.
Because any statement in a program can change the value of a global variable debugging becomes a
lot more difficult since you have to track down every statement that accesses the global variable to
find the error.
Some other good reasons are: Global variables make a program harder to understand--rather than
simply understanding the purpose and workings of a single module and what the variables place is in
the process, with a global variable you need to understand every part of the program that accesses
the global variable. Also using a global variable makes reusing a module that accesses it in a different
program more difficult. The module would likely have to be redesigned to it doesnt depend on the
global variable.
3.21 What is a global constant? Is it permissible to use global constants in a program?
A global constant is a named constant that is available to every module in the program. Because the
value of a global constant cannot be changed it doesnt present the same problems as a global
variable and can actually simplify making changes to a program. (Examples would be an interest rate
or tax rate). So yes, it is permissible to use global constants in a program.

S-ar putea să vă placă și