Lab 7: Function Prototypes and Implementations
Concepts
Focus on one concept in this assignment: declaring function prototypes above main with implementations below main.
Declaring Functions
We have mentioned how functions can be declared in three ways:
- Entire definition above main.
- Prototype above main with implmentations below.
- Prototypes and implementations defined in external files.
In previous assignments, you defined the entire function above main. In this assignment, you will refactor (modify/improve) a program such that the function prototypes are declared above main and the implementations of those functions are below main.
Here is an example:
void main() {
// fancy stuff
cout << sum(2, 1) << endl;
}
int sum(int x, int y) {
return x + y; // very exciting
}
Notice how the prototype of the function is declared above main and the full implementation of the function is beneath it. Also notice how the prototype matches part of the function implementation exactly (Be sure to review "function anatomy" if this is confusing.)
You might ask, "Why the heck can't I just have the whole definition below main?" Remember, the computer "reads" your program procedurally, one line after the next. The function protototype is like telling the machine, "Hey computer, I have a function called sum, and I'll tell you how it works later." Without telling the machine about sum first, when the machine reads main and sees the line containing sum(2, 1) it would freak out: "Wha? What's this thing called sum? You didn't tell me about this!" and therefore would not be able to compile the instructions in main.
You might also ask, "Why do this? Why not just put the whole implementation above main?" When someone (your programming date) is reading your program and wants to know what it does, he or she might first look for main. main describes the "main story" about what your program does (and alludes to whether or not you deserve a second date). If your program has, say, ten function definitions, the person reading your code needs to scroll down and hunt for your main implementation. Wouldn't it be better to briefly "summarize" the functions above main (via prototypes), resulting in main being much more noticeable? (It is better.)
This benefit starts to degrade when we have, say, fifty functions. You might argue that having fifty function prototypes above main also gets in the way of directly reading main, and you would be totally right. At this point, we would extract all this function drama in to external files, such that the only thing inside main.cpp is, you guessed it, main. We'll talk more about using external files later.
Function Prototypes vs. Implementations
A function prototype tells the machine "what" a function is (it's name, the parameters it expects, and the return type). An implementation tells the machine "how" the function works. Given the function implementation below, can you determine what the prototype should be?
cout << "Bwwwhhhuuuuuuuuh! Bhwhuh!!\n";
}
The prototype is the same as the implementation's "header". In other words, to declare the prototype, just replace everything between and including the code block from { to } with a semicolon. Like this:
That's it. Remember, the function prototype and header should match, exactly, always.
(A note to the advanced: yes, this "rule" can be bent, since we can omit parameter names from the function prototype, but this isn't generally recommended.)
Instructions
There's nothing more romantic during this cold winter than warming up to Visual Studio with someone you care about. The glow of your screen can provide some seriously intimate mood lighting. Don't deny it!
In this assignment, we're going to revisit the dating diary assignment, and simply refactor it by placing the prototypes of the functions you created above main and the implementations below.
Recommended Steps
Here are the steps you should follow to keep this assignment as painless as possible.
- Create a copy of your 16_datingDiary folder.
- Rename the new folder 17b_datingDiaryExternal.
- Open the file 17b_datingDiaryExternal/16_datingDiary.sln (don't worry about the fact that the project file is called 16_datingDiary.sln. This is ok.)
- Using Visual Studio, edit main.cpp.
We recommend that you cut your functions above main and simply paste them below main. Now, simply declare the prototypes for each function above main.
You'll know you are done when your program compiles and runs normally after making these changes.
Requirements and Rubric
Your program must have prototypes above main for prompt, captureString, captureInt and save.
Your program must have implementations below main for prompt, captureString, captureInt and save.
You should not modify main at all in this assignment.
A friendly message from The Terminator, our grading program
*bzzzt* It's getting lonely back here. Do you have any robot friends you can hook me up with?
I will check for the following:
Your program must define prototypes above main and implementations below main.
I'm sma *bzzzt* smart, funny and *bzzt* I like long walks on the beach.
This work is worth 90 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 5 | |
Correct submission of src directory as a .zip file. | 5 | |
Correct declaration of prompt above main. |
10 | |
Correct declaration of captureInt above main. |
10 | |
Correct declaration of captureString above main. |
10 | |
Correct declaration of save above main. |
10 | |
Correct implementation of prompt below main. |
10 | |
Correct implementation of captureInt below main. |
10 | |
Correct implementation of captureString below main. |
10 | |
Correct implementation of save below main. |
10 |
Concepts Exercised: functions, abstraction, readability, style, declaring facts, repeating tasks, program design, fun, I/O, using libraries.
© 2012 Yong Joseph Bakos.