Welcome to Lesson 18!
Learning Objectives
By the end of today's class, you should know...- How do you write a function prototype?
- Where should the prototype be placed inside your program?
- What is the purpose of a function prototype
Announcements
- Midterm 2 after a short lesson and the break
- Don't forget Lab 9 due Friday and Assignment 17 due Tuesday
Function Prototypes
- C++ allows you to declare functions without defining them
- Function declarations (prototypes) have the function heading without the function body
- The general syntax for declaring a function is:
returnType functionName(parameter1, ..., parametern);
- Where:
- returnType: the type of the value returned
- functionName: the name you make up for the function
- parameterx: the input values, if any
- As an example, we can declare a function to calculate the square of a number like this:
double square(double number);
- By declaring a function, the compiler can resolve a function call made inside
main()
- Thus, we can reorganize our programs to place function definitions after main()
- For now the use of function prototypes is optional
- However, there are times in C++ when you need to use function prototypes
- Note that if you use function prototypes, you place the block comments before the prototypes and not the definitions
- You can see this new function organization in the following example
Example Program with Function Prototypes
|
#include <iostream>
using namespace std;
int square(int number);
void printSquare(int length);
int main() {
cout << "Enter a number to square: "; int side; cin >> side; cout << "The square of the number is " << square(side) << endl; cout << "As you can see for yourself!\n"; printSquare(side); }
int square(int number) {
int result = number * number;
return result;
}
void printSquare(int length) {
for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { cout << "*"; } cout << endl; }
|
Note that the function signatures must match in regards to data types of the parameters and return values!
Okay to do:
//prototype
void printDate(int month, int day, int year);
//function
void printDate(int m, int d, int y) {
cout << "The date: " << m << "/" << d << "/" << y << endl;
return;
}
Not okay to do:
//prototype
void formatDate(int month, int day, int year);
//function
void formatDate(double month, double day, double year) {
cout << "The date: " << month << "/" << day << "/" << year << endl;
return;
}
Not okay to do:
//prototype
void formatDate(string month, string day, string year);
//function
string formatDate(string month, string day, string year) {
return month + "/" + day + "/" + year;
}
Programming Style Requirements for Functions
Commenting Functions
- Good programming style dictates that each function have a comment, stating what it does.
- There is no universal standard for comment layout.
- Often in C++, you will see a comment written underneath each function prototype
- The following example has commented functions
Example Program with Commented Functions
|
#include <iostream>
using namespace std;
double square(double number); //Multiplies a number by itself
void printDate(int month, int day, int year); //Prints a date in the m/d/y format
int main() {
double number = 5; double result = square(number); cout << "The square of 5: " << result << endl; cout << "The square of 3: " << square(3) << endl; int month = 4; int day = 2; int year = 1845; printDate(month, day, year); printDate(3, 26, 2015); return 0;
}
double square(double number) {
double result = number * number;
return result;
}
void printDate(int month, int day, int year) {
cout << "The date: " << month << "/" << day << "/" << year << endl;
return;
}
|
Activity 18.1: Prototypes and Comments (10 pts)
- In the text box under Activity 18.1 on Canvas, write the prototypes for the following functions.
- Then below each prototype write a comment describing what the function does.
double areaTriangle(double base, double height) {
double area = 0.5 * base * height;
return area;
}
string myName(string firstName, char initial, string lastName) {
string fullName = firstName + " " + initial + ". " + lastName;
return fullName;
}
bool isLeapYear(int year) {
if (year % 4 == 0) {
return true;
} else {
return false;
}
}
- When you are finished, submit to Canvas.
Midterm 2
|
|