next up previous
Up: Return

Modular Programming

Group: Captain: Recorder: Reflector:

Motivation

While the program is growing, it also becomes more difficult to read, understand, and debug. By breaking the main program apart into pieces, functions, you can improve clarity and debugging.

It also allows you to reuse code, in the same program or in several programs. Taking a working subroutine and applying it to as many places as possible is an effective way to deal with larger programming tasks.

Understanding functions is essential to make you a better programmer, hence a more desirable engineer.

Objectives

1.
Learn about functions.
2.
Learn about character variables.
3.
Learn about projects.
4.
Improve program neatness.
5.
Strengthen previous knowledge.

Performance Criteria

1.
Success in modifying your program so that it only asks once for the the costs of brass and steel, however many parts the user computes. Do that by initializing these variables to zero. Then do not ask for the steel cost unless it is still zero, and the value is needed for the current part. The same for the cost of brass.

2.
Success in taking part of the program out of function.cpp and into a function ... menu (...) in a new file menu.cpp. The part that you should take out is writing the menu to the screen and getting the menu letter from the user

Function menu should return the letter selected by the user to the main function. Main will still do the processing appropriate to that choice, (input a material cost, compute a part, quit, or print an error message.) (25%)

Hint: You will need to declare storage locations for the letter, say menu_choice both inside main and inside menu.

Hint: Function menu must return a character to the main function. What does that mean for the declaration/prototype of function menu?

Hint: menu will be called in the main function using an assignment statement.

Hint: Function menu does not receive any information from the main function. What does this mean for the declaration/prototype of function menu?

Hint: Unless you can answer the first three hints, do not even try to do any programming. See lesson 9 on functions if you cannot answer them.

3.
Success in splitting up your main function further as follows: Take the part of the main program that prompts the user for a part's steel and brass weights and the cost of these materials, and that then computes and prints the part's total material cost. Put all of this into a function part_process. Add it to the project file list. (25%)

Hint: Function part_process does not need to return anything to the main program, since it should already print out the part's material cost itself.

Hint: Function part_process will of course need the costs of brass and steel. Figure out the right way to get these from main into part_process (Lessons 10 and 14).

Hint: Do at least two parts and check that you do not have to reenter the costs of steel and brass the second time.

4.
Success in E-mailing the three C++ files to the instructor as a single mailing.

5.
Success in producing a clearer, better commented program that is more easy to read and understand by a stranger than the programs of the instructor and any other group in the class. Success in producing a neater, better looking program than the instructor or any other group in the class. Success in combining files into projects and E-mailing final source codes as ASCII text to the instructor. (20%)

6.
The knowledge demonstrated by your definitions of the vocabulary. (10%)

7.
The depth of understanding demonstrated by your answers to the critical thinking questions. (20%)

Resources

1.
Your class notes.
2.
The textbook.
3.
The online help facilities of Windows and the C++ compiler.

Plan

1.
Skim through the lessons ``Getting Started With Functions'', (``Changing Parameter Values''), and ``Using C++ References'' of the book. Check the activity notes on the web page for what you need to know.
2.
Do the programming tasks that are performance criteria.
3.
Define the vocabulary.
4.
Answer the critical thinking questions.

Vocabulary

Select the best answer:

1.
Function.
(a)
The normal and specific contribution of a bodily part to the economy of a living organism.
(b)
The purpose of a group of source statements.
(c)
Some source statements grouped together.

2.
Parameter.
(a)
A number shared between the called function and the calling function
(b)
A number or memory location shared between the called function and the calling function
(c)
A memory location shared between the called function and the calling function

3.
Return value.
(a)
A number or memory location that is always given to the called function when this function is called.
(b)
A number or memory location that is always given back to the calling function by the called function when it has finished its job.
(c)
The amount of cash Wallmart gives you back when you return unused paint to the store.

4.
Return type.
(a)
The act of pressing a plus-size key on a computer keyboard.
(b)
The variable returned by a called function to the caller when the called function is done.
(c)
The sort of variable that comes out of a called function.

5.
Scope.
(a)
Means: in which functions a variable can be used.
(b)
The mouthwash of choice of C++ programmers.
(c)
The locations or functions in which a variable has been declared using a type-declaration of local or global validity and accessibility.

6.
Pass-by-value.
(a)
Giving the value of a variable to a called function.
(b)
Giving the storage location of a variable to a called function.
(c)
Giving students a passing grade based on their lab and test performance.

7.
Pass-by-reference.
(a)
Giving the value of a variable to a called function.
(b)
Giving the storage location of a variable to a called function.
(c)
Giving students a passing grade based on their letters of recommendation.

8.
Function prototype.
(a)
A first draft of a function's source code, before being debugged.
(b)
An absolutely useless line or few lines in a file that indicate roughly how some function looks. Only necessary since the compiler is too stupid to check other open windows and files in the directory for the function.
(c)
A valuable part of program that defines a function header, arguments, and source code statements. It allows the function to be compiled correctly.

Critical Thinking Questions

Select the best answer:

1.
Without using global variables, what is the proper way to get information into a function from the caller?
(a)
Using the return type.
(b)
Using the return value.
(c)
Using a parameter.

2.
Without using global variables, what are the proper two ways to get information out of a function to the caller?
(a)
Using the return type or return value.
(b)
Using a parameter type or return type.
(c)
Using a parameter value or return value.

3.
What is produced by the following program?
    #include <iostream.h>
    void three(void) { cout << "3 "; }
    void two(void) { cout << "2 "; three(); }
    void one(void) { cout << "1 "; two(); }
    void main(void) { cout << "Counting "; one(); }
(a)
The program will produce a compilation error.
(b)
3 2 1 Counting
(c)
Counting 1 2 3

4.
What is produced by the following program?
    #include <iostream.h>
    void Jack(void);
    void John(void) { cout << "John "; Jack(); }
    void Jane(void) { cout << "Jane "; }
    void Jill(void) { cout << "Jill "; John(); }
    void Jack(void) { cout << "Jack "; Jane(); }
    void main(void) { cout << "Hi "; Jill(); cout << "!";}
(a)
John Jack Jane Jill John Jack Jane Hi Jill!
(b)
John Jane Jill Jack Hi !
(c)
Hi Jack Jill Jane John !
(d)
Hi Jill John Jack Jane !

5.
Why is the Jack definition done twice in the above program, and John, Jane, and Jill are not?
(a)
The instructor is an idiot.
(b)
This is necessary since Jack is the last function before the main function.
(c)
Are you an idiot? Jack is not defined twice!

6.
What happens if you call the following function:
    void hi(void) {
      cout << "Hello\n" ;
      hi();
      }
(a)
It comes running.
(b)
It will produce a compilation error since recursion of functions is not allowed.
(c)
It will keep writing Hello lines to the screen.

7.
What is produced by:
    #include <iostream.h>
    void set(void) {
      int a;
      a=1;
      }
    void main(void) {
      int a;
      a=0;
      set();
      cout << a;
      }
(a)
It will produce a compilation error since we did not put void between the parentheses when set was called inside main.
(b)
0
(c)
1

8.
What is produced by:
    #include <iostream.h>
    int a;
    void set(void) {
      a=1;
      }
    void main(void) {
      a=0;
      set();
      cout << a;
      }
(a)
It will produce a compilation error since declarations must be immediately behind the opening curly brackets of functions.
(b)
0
(c)
1

9.
Is the following code legal in C++? What does it produce?
    #include <iostream.h>
    void out(int houses, int horses) {
     cout << houses << " " << horses;
      }
    void main(void) {
      int boats=10, sheep=2;
      out(boats, sheep);
      }
(a)
No, this is a syntax error.
(b)
No, you must put void before out in main.
(c)
No, you must put int before boats and sheep in the call to out in main.
(d)
Yes, but it is lousy coding. It produces houses horses
(e)
Yes, but it is lousy coding. It produces boats sheep
(f)
Yes, but it is lousy coding. It produces 10 2

10.
What is produced by the following program?
    #include <iostream.h>
    void show(float a) {
      cout << a << endl;
      }
    void main(void) {
      float a=1, b=2;
      show(b);
      }
(a)
This will produce a compilation error. The argument must be a, not b
(b)
1
(c)
2

11.
What is produced by:
    #include <iostream.h>
    void out(int a, int b) {
      cout <<  a << " " << b << endl;
      }
    void main(void) {
      int a=1, b=2;
      out(b,a);
      }
(a)
This will produce a compilation error. The arguments are reversed.
(b)
1 2
(c)
2 1

12.
What is produced by (may not work with MS C++):
    #include <iostream.h>
    int out(int a, int b) {
      cout << a << " " << b << " ";
      return a+b;
      }
    void main(void) {
      cout << "(" << out(1,2) << ")" << endl;
      }
(a)
This will produce a compilation error. You cannot use a function in a cout statement.
(b)
(1 2)
(c)
(3)
(d)
(1 2 3)
(e)
(1 2 ) 3
(f)
(a b ) 3

13.
What is produced by:
    #include <iostream.h>
    int fun(int i) {
      if (i==1) return 1;
      else return i*fun(i-1);
      }
    void main(void) {
      cout << fun(1) << " " << fun(2) << " " << fun(3) << " "
           << fun(4) << " " << fun(5) << endl;
      }
(a)
0 0 0 0 0
(b)
1 2 3 4 5
(c)
1 2 6 8 10
(d)
1 2 6 24 120

14.
What does the following code produce:
    #include <iostream.h>
    int ratio(int a, int b) {
      return a/b;
      }
    void main(void) {
      int a=1, b=2;
      cout << ratio(a,b) << " " << ratio(b,a);
      }
(a)
ratio(a,b) ratio(b,a)
(b)
a/b a/b
(c)
0 0
(d)
0.5 0.5
(e)
0.5 2
(f)
0 2
(g)
2 2

15.
What happens for:
    #include <iostream.h>
    void out(int i) {
      cout << i << endl;
      }
    void main(void) {
      out(1.5);
      }
(a)
This will produce a link error. The argument must be an integer.
(b)
It will print 1.5.
(c)
The computer will stream the I/O to an h.

16.
What is produced by:
    #include <iostream.h>
    void swap(int a, int b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << " --- ";
      swap(a,b);
      cout << a << " " << b << endl;
      }
(a)
1 2 --- 1 2
(b)
1 2 --- 2 1
(c)
1 2 --- b tmp

17.
What is produced by:
    #include <iostream.h>
    void swap(int &a, int &b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << " --- ";
      swap(a,b);
      cout << a << " " << b << endl;
      }
(a)
This will produce a compilation error. There is an & before the a and b.
(b)
This will produce a compilation error. a and b must be reference variables in main.
(c)
This will produce a compilation error. a and b must be aliases in main.
(d)
1 2 --- 1 2
(e)
1 2 --- 2 1
(f)
1 2 --- b tmp

Exercises
1.
Create void error_msg(char *msg) function so that, for example, error_msg("This is a test") or error_msg("You made a mistake!") will print out these messages along with a beep and a new line. Use your function to print out the ``You entered an invalid choice'' message in the menu.
2.
Put inputting the costs of brass and steel into functions named get_brass_cost and get_steel_cost. These should take no parameters.
3.
Create a function input_positive_float which inputs a float from the user, but does not accept a negative of zero value. Use this function to obtain the costs of brass and steel from the user.
4.
Create a similar function input_nonnegative_float to input the weights of steel and brass of the part.
5.
Print the date at the start of your program.
6.
Write an overloaded error_msg function that can either print just a character string, or a character string followed by a floating point number. Use it for the error messages when the user inputs an invalid number.

Evaluation

1.
Give two strengths of your group.
2.
Indicate two areas for improvement.
3.
What two new insights did your group obtain?
4.
Give two strengths of the class so far.
5.
Indicate two areas for improvement of the class.
6.
What two new insights did you obtain about the class?

next up previous
Up: Return
Author: Leon van Dommelen