CS 150 - Intro to Programming - Fall 2001
Lab Exercise 7

Topics:

Part 1 - Program Development using functions - Stubs and Drivers

We will start today's lab with a discussion of how to develop a program that makes several function calls. Consider the general problem for presenting the user with a menu of choices and then taking one of several actions depending on the user choice. For this example I want to develope a program that will: find the minimum of two ints; find the maximum of two floating points; or print a line of symbols. We will use functions to help us write this program. By breaking the program into several functions I can focus on the details of implementation one at a time. By breaking up the functionality of my program it will be easier to implement, and change or extend later. Using functions help make the logic of a program more apparent. For this problem we will need a function to print the menu and one function for each task to be performed. We will start by implementing the menu function - then develope stubs and driver for for each function and finally implement the functions (write the body).

We will use three code samples to illustrate this process:
funDevelopProcess1.cpp | funDevelopProcess2.cpp | funDevelopProcess3.cpp


Part 2 - Using the process

Convert the program you wrote in lab6 part 3 to use functions (yourLastNameLab7Part2). Your program should have at least 5 functions in addition to main. One for the menu and 4 functions to compute an area, volume, or hypotneuse. You can use if /else statements or a switch statement in main to execute the user request. Your program must get any input "outside" the function that does the calculation (i.e. before the function call). Send the values to the function, and have the function return the area, volume or hypotneuse then print the result of the function call. Make sure to include pre and post conditions when prototyping the functions and to place a comment block before the function body (implementation). Use the development process demonstrated above. Make sure you do one step at a time - it is much easier in the long run to use stubs for function development. Use debugging statements and the debugger if you have problems getting things to work.

Note: Why get user input outside the functions??. A function should be designed to do only one thing and do it well - this makes them easier to "reuse" in different programs. For example if I write a function called sortThree(int, int, int) that will sort three integers. If I prompt the user for input in the function and later need to read the data from a file, I would have to modify sortThree(int, int, int). Consider the built in character and math functions we have studied for more examples.

Remember to use functions:


Part 3 - Write your own function

Write a C++ program (yourLastNameLab7Part3) that asks the user to enters three floating point numbers, than calls a function that returns the largest of the three numbers and a function that returns the smallest of the three numbers, and prints a message to the user stating which number is the largest and which is smallest.


What to turn in - Note no .mcp file is needed

Remember to: