CIS 150 Functions I

Objectives

  • Create functions to solve problems and modularize programs
  • Send data to functions for processing
  • Use data returned from functions
  • Create function prototypes

Overview of functions

Using functions can make many programs easier to code and maintain, but they introduce a certain amount of complexity when first encountered. It is important to map out the flow of processing and data when breaking a program down into functions so that you can visualize clearly where the data is being used and what values or variables are being shared between functions.

Sharing data is the most important concept when first dealing with functions. The basic syntax for functions is the same as you have used when coding main(). main() is a function and will often be one of many that your program will have. You have called functions when you have used rand(), pow(), and getline(), so calling or using functions is not new either. The big difference now is that you will be defining or writing functions and then calling or using them in the same program. All variables will remain local to a function. We will not use any variables with a global scope. This means that if two or more functions use the same value, or create a value through calculations, we must have some way of sharing those values between those functions. Charts are sometimes used to show interactions among functions and to diagram the flow of data from one function to another.

Function example

Take the problem of a program that calculates the area of a rectangle. This program gets the width and height from the user. We want to make sure that the width and height are greater than 0. This problem provides a simple example of how a function A function can be written to get input from the user and to make sure it meets requirements. Instead of writing the code twice (once for the height and once for the width), the function can be used for both.

In this example, the program is broken down into four functions:

  • control (main)
  • input (getInput)
  • processing (calcArea)
  • output (outputArea)

The hierarchy chart for this program looks like this:

Hierarchy chart for program

Each function is its own little program. You have to decide what data it needs to perform its function, and what data (if any) must be returned. For this particular problem, here's how it will work:

  • main
    • input: needs width and height from user, uses getInput to get them
    • output: needs to display area, uses outputArea
  • getInput
    • input: needs prompt and minimum value to accept, get input from user
    • output: displays prompt, possibly an error message, and needs to return a valid number
  • outputArea
    • input: needs width and height
    • output: needs to display area, uses calcArea
  • calcArea
    • input: needs width and height
    • processing: calculates area using width and height
    • output: needs to return area

Finished function example

Once you understand how the data will flow between functions, it is easier to understand how to write and use a function. The function header is the first line of code. It specifies the data type to be returned (or void if nothing is being returned), the name of the function, and a list of parameters with their data types (or nothing if there are no parameters). So here is what the four functions headers will look like:

  • int main()
  • double getInput(string prompt, double minVal)
  • double calcArea(double width, double height)
  • void outputArea(double width, double height)

Any function that main() will call should be written after main(), but will require a prototype written before main(). The prototype looks just like the header, except it will not have a function body. It will just end with a semicolon.

Here is the finished program:

#include <iostream> #include <string> using namespace std; // These are the function prototypes. // They are required so that main knows how to handle the functions // when it sees them called in its code. Function prototypes just // specify a function's name, what data type it returns, and the // data types of any arguments sent to it. double getInput(string prompt, double minVal); double calcArea(double width, double height); void outputArea(double width, double height); int main() { double width; double height; // A string and a double are sent to getInput. // Whatever is returned (a double) is stored in width. width = getInput("Enter width: ", 0.0); // A string and a double are sent to getInput. // Whatever is returned (a double) is stored in height. height = getInput("Enter height: ", 0.0); // Two doubles are sent to outputArea. // Nothing is returned, so outputArea probably displays something. outputArea(width, height); return 0; } double getInput(string prompt, double minVal) { double val; do { cout << prompt; cin >> val; if (val < minVal) { cout << "Error: Minimum value is " << minVal << endl; } } while (val < minVal); return val; } double calcArea(double width, double height) { return width * height; } void outputArea(double width, double height) { cout << "The area is " << calcArea(width, height) << endl; }