Welcome to Lesson 17!


Learning Objectives
By the end of today's class, you should know...
  • What is a function prototype and what is its purpose?
  • Where do you place a function prototype in your program?
  • What is the syntax of a function prototype?
  • How do you properly comment a function?
  • How can you call one function inside of another function?

Announcements

  • Midterm 2 after a short lesson and the break
  • Homework posted, due Tuesday
  • No Lab on Friday due to Thanksgiving holiday!
  • Tomorrow's Office Hour will be a review session for 36a. Please email me with questions as I will be unavailable at that time.


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 printDate(int month, int day, int year);

//function
void printDate(double month, double day, double year) {
    cout << "The date: " << month << "/" << day << "/" << year << endl;
    return;
}

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 17.1: Prototypes and Comments (10 pts)

  • In a text file called prototypes.txt, 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 your prototypes.txt to Canvas.


Functions Calling Functions
  • Functions may call other functions
  • Within the body of one function, we can call another function call
  • Functions can call other functions as often as needed
  • We are already doing this when main() calls a function
  • The following program calls a "helper" function to help calculate the BMI.
  • Because calculating the BMI is a somewhat complicated process, it is helpful to create a second function to do part of the work for us.
  • The heightToSquareInches function handles turning the height from feet and inches (such as 5'8") into inches squared (such as 68"2).

Example of Functions Calling Functions


#include <iostream>
using namespace std;

double calculateBMI(int feet, int inches, double weight);
//Calculates a user's Body Mass Index

int convertFeetInches(int feet, int inches);
//Converts height in feet and inches to height in inches

int squareInches(int inches);
//Squares the height in inches

int main() {
    const double WEIGHT = 135.5;
    const int HEIGHT_FEET = 5;
    const int HEIGHT_INCHES = 8;
    double bmi = calculateBMI(HEIGHT_FEET, HEIGHT_INCHES, WEIGHT);
    cout << "Your BMI is: " << bmi << endl;

    return 0;
}

double calculateBMI(int feet, int inches, double weight) {
    int heightInches = convertFeetInches(feet,inches);
    int heightInches2 = squareInches(heightInches);
    double bmi = 703 * weight / heightInches2;
    return bmi;
}

int convertFeetInches(int feet, int inches) {
    return 12 * feet + inches;
}


int squareInches(int inches) {
    return inches * inches;
}


Wrap Up
  • Answer the questions from today's learning objectives

Upcoming Assignments

  • Assignment 17 due Tuesday on Canvas
  • No Lab on Friday due to holiday


~Happy Thanksgiving!~