Lab #6: Using Arguments & Parameters

Video Modules 10, 11, and 14

Due: In lab for full credit, or by March 2, 1997 by 10:00 pm for 70% credit.

Goal of Exercise

This exercise will illustrate the use of arguments and parameterswithin a VB/DOS program. Arguments are used topass information to a SUB or function so that the SUB or function can carry out its task based on the information that it has received. We have been using arguments since the first lab --- like the SUB DisplayMessage, which accepts one string argument. So, we know how to send data to a SUB or function, but how do we receive information? Parameters are used by the called SUB or function toreceive information from the module that called it.

In this lab, you will be passing numericconstants and stringconstants as arguments to SUBs which you will write. Programs which effectively use arguments are easy to understand and easy to modify when a change becomes necessary.

Background

Current computer technology is changing very rapidly, in fact, a new processor technology is released approximately every nine months! This trend makes it very difficult to assess the value of a computer years after it was purchased. Don’s Computer Depreciation Estimation Service has been established to help customers determine a good estimate of how much their system is worth after it has aged. You have been hired to write a program that allows a customer to enter their computer model, age in years, and its original cost, to obtain an estimated value of its current worth.

** Read the ENTIRE handout before you begin.

The Program

You have been asked to write a program that accepts three pieces of information about a computer system. In an effort to keep the program modularized, each level of modules will only be responsible for accepting one additional piece of data about the user’s computer system.

The Main module should prompt the user for the brand name of his/her computer and call a corresponding SUB for each of the models. The four models your program should accept are : PC, Mac, Next, and Amiga. All other model names will generate an error message.

At the next level, you need to write four SUBs, one for each of the computer models listed above. The purpose of these four SUBs is to receive the age of the computer from the user. In order to make our program simpler, we’ve decided that any computer with an age under five years will be considered three years old, and any computer five years or older will be considered to be seven years old. This decision was made based on the fact that most people keep their computer at least three years, but never longer than seven years.

At the last level, you will need to write one SUB, that receives the original cost of the computer from the user. Again, we’ve made some simplifying assumptions : if the computer cost less than $1000, we will assign it a value of $750, but if the system cost $1000 or more, we will assign it a value of $1500.

Once the original cost has been entered, your program should pass the three pieces of information along to the SUB DisplayCompValue, which has been pre-written for you. This SUB will display all of the information collected about the machine, and will display an estimated current value due to depreciation.

The following tables may help you in writing your program :

Module Name(s) / Purpose / Number of Parameters
Main / Get brand name from user / N/A
4 Brand Name SUBs / Get machine age from user / One
1 SUB at next level of structure chart / Get original cost from user / Two
DisplayCompValue (pre-written) / Display estimated current value / Three
If the machine is ... / Assign it an age of ...
Less than five years old / Three years
Five or more years old / Seven years
If the machine cost ... / Assign it a price of ...
Less than $ 1000 / $ 750
$ 1000 or more / $ 1500

Additional Information

The pre-written module, DisplayCompValue, is available from the directory

J:/VBSTD3/LABS/COMPVAL.BAS

You need to copy this to your AFS space using File Manager, and add the file to your project using

VB-DOS. You’ll find this command under the File menu in Visual Basic. Ask your TA or Grader for

more details.

You must Hand-In your program using the Hand-In program. Make sure you have a header at the top of your Main module!

Parameters can sometimes be confusing- here is an example of how to use parameters. We will write a SUB called Display2XMessage which accepts a string to be displayed and displays it twice.

(a useless example, but it gets the point across)

Here is an example of how the SUB is called:

CALL Display2XMessage ("Hello, World")

Here is what the SUB looks like:

’message$ is going to be the name of the memory location where VB/DOS

’has stored the data. You can give it any name you want

SUB Display2XMessage( message$ )

’in the next two lines, we just merely use the name of the memory

’location to reference the data...

‘notice that you can check the value of a parameter by using it with

‘a CALL to DisplayMessage

CALL DisplayMessage( message$)

CALL DisplayMessage( message$)

END SUB

Here is the prewritten module, available from J:\VBSTD3\LABS\COMPVAL.BAS

Please note that you may choose to copy the file from the J drive to your AFS space, or if you wish, you may type in the module as follows. Please take note that, where indicated, some lines that appear separate here need to be typed all on one line in Visual Basic.

** You ARE NOT expected to understand some of the concepts used in this module! Consider it similar to a library module, only in this case, you get to see the code involved.

SUB DisplayCompValue (cost, age, Computer$)

SELECT CASE LCASE$(Computer$)

CASE IS = LCASE$("PC")

RANDOMIZE TIMER

LET X = INT(RND * age) + 1.5

LET Y = cost / X

‘ NOTE!! The following three lines are to be typed on one line in Visual Basic!!

MSGBOX "Your " + FORMAT$((age), "#") + " year old " + Computer$ + " was once " + FORMAT$((cost),

"$#,###") + " but is now worth " + FORMAT$((Y),"$#,###.##"),0, "Don’s Computer Service"

CASE IS = LCASE$("MAC")

RANDOMIZE TIMER

LET X = INT(RND * age) + 47

LET Y = cost / X

‘ NOTE!! The following three lines are to be typed on one line in Visual Basic!!

MSGBOX "Your " + FORMAT$((age), "#") + " year old " + Computer$ + " was once " + FORMAT$((cost),

"$#,###") + " but is now worth " + FORMAT$((Y),"$#,###.##"),0, "Don’s Computer Service"

CASE IS = LCASE$("AMIGA")

RANDOMIZE TIMER

LET X = INT(RND * age) + 13

LET Y = cost / X

‘ NOTE!! The following three lines are to be typed on one line in Visual Basic!!

MSGBOX "Your " + FORMAT$((age), "#") + " year old " + Computer$ + " was once " + FORMAT$((cost),

"$#,###") + " but is now worth " + FORMAT$((Y),"$#,###.##"),0, "Don’s Computer Service"

CASE IS = LCASE$("NEXT")

RANDOMIZE TIMER

LET X = INT(RND * age) + 3

LET Y = cost / X

‘ NOTE!! The following three lines are to be typed on one line in Visual Basic!!

MSGBOX "Your " + FORMAT$((age), "#") + " year old " + Computer$ + " was once " + FORMAT$((cost),

"$#,###") + " but is now worth " + FORMAT$((Y),"$#,###.##"),0, "Don’s Computer Service"

CASE ELSE

MSGBOX Computer$ + " is not a valid computer"

END SELECT

END SUB

Names:______Section:______

Pre-Exercise Questions (4 points)

1.Explain the overall flow of logic in this programming assignment. For example, explain what needs to happen at each of the modular levels in the program flow. Also include in your answer the types of constructs used in each module (SELECT CASE, LOOP, ....). Your answer should be a few sentences written in English, no code is needed. You should not simply repeat what has been stated in the handout. (2 points)

2.Draw the structure chart for this program. (1 point)

3.How many total SUBs will you have to write for this program? (Don’t count the Main module) (1 point)

Names:______Section:______

Post Exercise Questions (4 points)

These questions can be answered in REMarks at the end of your program, or as otherwise noted by your TA.

1.How many SUBs would you have needed to write if you didn’t use parameters? Briefly explain your reasoning. (2 points)

2.List the arguments that you used for each of the modules that you wrote. (1 point)

3.List the parameters that you used for each of the modules that you wrote. (1 point)