Welcome to Lesson 16!


Learning Objectives
By the end of today's class, you should know...
  • What is a void function and how does it differ from a non-void function?
  • 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 next class
    • Lesson 9 through Lesson 15
  • There is homework for tonight due Tuesday!
    • Practice quiz assignment to help you prepare for the midterm
    • No in-class quiz today
  • Lab 7 due Friday - don't forget!
  • Last day to drop with a "W" is tomorrow
    • Check your grade on Canvas
    • Come talk to me
  • De Anza Women in CS and CIS Meeting Tomorrow
    • Noon-1:15pm in ATC 204
  • Developer's Guild Meeting Tomorrow
    • 1:30-4:00pm in ATC 311
    • Projects in C++, Java and Machine Learning - or start your own!

Review Activity

With a partner, answer the following questions:

  • Label the components of the following function:

double calc_area_circle(int radius) {

    double area = 3.14 * radius * radius;

    return area;

}

Function Name:

Return type:

Parameter(s):

  • Write a correct function call for the above function, passing it the value of 9, and storing the result in the cArea variable:

         double cArea = ???????????????????;

    cout << "The area of the circle with radius 9 is: " << cArea << endl;

  • What is output by the following program? (Do not run the code -- work it out by hand)

#include <iostream>
using namespace std;

int mystery(int param) {
    cout << "param=" << param << endl;
    param = param * 2;
    return param;
}

int main() {
    int num = 2;

    cout << "At first, num=" << num << endl;
    int result = mystery(num);
    cout << "After calling, num=" << num << endl;
    cout << "And result=" << result << endl;

    return 0;
}
  1. At first, num=2
    param=2
    After calling, num=4
    And result=4
    
  2. At first, num=2
    param=4
    After calling, num=4
    And result=4
    
  3. At first, num=2
    param=2
    After calling, num=2
    And result=4
    
  4. None of these


  • Write the following functions:
  • areaRectangle
    • Takes in a double for the length and width
    • returns the area of the rectangle as a double
  • areaTriangle
    • Takes in a double for the base and height
    • returns the area of the triangle as a double
  • mpg
    • takes in an integer for the miles and the gallons
    • returns the miles per gallon as a double

Void Functions

  • Previously we looked at functions that returned one value
  • Functions returning a value use a return statement
    return result;
  • A function that returns no value is called a void function
  • In C++, void functions are defined like functions that return a value
  • However, the keyword void replaces the return type
  • For example, what do you notice that is different about the following?
    void displayDegrees(double degreeFarenheit) {
        double degreeCelsius = 5.0 / 9 * (degreeFarenheit - 32);
        cout << degreeFarenheit
             << " degrees Fahrenheit is equivalent to "
             << degreeCelsius << " degrees Celsius." << endl;
        return;
    }
    
  • There are only two differences between definitions for void functions and other functions:
    • void return type
    • return statement is optional and does not specify a value if used
  • If no return type is specified, the function returns after executing the last statement
  • Here is an example program using the void function shown above

Example Program With a void Function


#include <iostream>
using namespace std;

void displayDegrees(double degreeFarenheit) {
    double degreeCelsius = 5.0 / 9 * (degreeFarenheit - 32);
    cout << degreeFarenheit
         << " degrees Fahrenheit is equivalent to "
         << degreeCelsius << " degrees Celsius." << endl;
    return;
}

int main() {
    double fTemperature;

    cout << "Enter a temperature in Fahrenheit: ";
    cin >> fTemperature;
    displayDegrees(fTemperature); //Notice function call without assigning result to variable

    return 0;
}

When to Write void Functions

  • When we use a non-void function, we are asking a question
  • The function returns a value in response to our question
    cout << sqrt(9.0);
    
  • When we use a void function, we are giving the computer a command
    displayDegrees(212);
  • We do not expect or receive an answer

Common Errors With void Functions

  • Note that we cannot call a void function from a cout statement
  • For example, the following causes a compile error:
    cout << displayDegrees(fTemperature); // NO!
  • The reason is that a void functions does not return a value and cout has nothing to print
  • Similarly, we cannot call a void function in an assignment statement:
    double temp = displayDegrees(fTemperature); // NO!
  • There is nothing to assign to the variable temp

Activity 16.1: Printing Squares (10 pts)

  • Remember our programs that used nested for loops to print out shapes.
  • Let's write a similar program with a function that prints squares of different sizes for our user.
  • Open up CodeBlocks and create a new C++ file called squares.cpp.
  • Then, copy and paste the starter code into your file, save it and run it to make sure everything is working properly.
#include <iostream>
using namespace std;

//Your function goes here

int main() {
    int length;

    while (length != -1) {
        cout << "I will print squares for you!\n";
        cout << "Please enter the length of one side of the square or -1 to quit: ";
        cin >> length;
        //code to call function

    }
    cout << "Thanks for \"square\" dancing with me!" << endl;

    return 0;
}
  • Now, write a function that prints squares called printSquares(). Your function should take in an integer argument for the length of one side of the square and should return nothing.
  • Call your function inside the while loop so that it will print out a square given the user input for the length of a side.
  • Run the program again. Does it print out a square?
  • When you are finished, upload your squares.cpp file to Catalyst.
  • The output of your program should look identical to the sample output below (except user input will vary).


Activity 16.2: Into the Void (10 pts)

  • Void functions are useful for printing out information in a particular format.
  • Let's consider dates and times.
  • In America, we use the 12 hour clock, but in Europe, the 24 hour clock is used. For example, in America, 8:30 at night is represented as 8:30pm, while in Europe, it is represented as 20:30.
  • In America, we write dates in this format MM-DD-YYYY. In Europe, dates are often written as DD.MM.YYYY
  • Let's write a program that uses void functions to format dates and times.
  • We will print each date and time in both the American and European formats for our user.
  • Open up CodeBlocks and create a new C++ file named dateTime.cpp.
  • Copy and paste the starter code below into your file:
/*
* Name(s)
* Section info
*/
#include<iostream>
using namespace std;


//write functions here

int main() {
    int year;
    int day;
    int month;
    int hour;
    int minutes;
    string dayEve;

    cout << "Welcome! This program will print dates and times in both the American and European styles!\n\n";

    cout <<"First, let's print a formatted date.\n\n";
   
    cout << "Please enter the current year: ";
    cin >> year;
    cout << "Please enter the current month: ";
    cin >> month;
    cout << "Please enter the current day: ";
    cin >> day;
    cout << endl;
  
    //call to the formatDateAmerican function here
    //call to the formatDateEuropean function here
   
    cout << "\nNow, let's print a formatted time.\n\n";
   
    cout << "Please enter the current hour: ";
    cin >> hour;
    cout << "Please enter the current minutes: ";
    cin >> minutes;
    cout << "Please enter whether it is \"morning\" or \"evening\": ";
    cin >> dayEve;
    cout << endl;

   
    //call to the formatTimeAmerican function here
    //call to the formatTimeEuropean function here

    cout << "\nBye! See you another day!" << endl;

    return 0;
}



  • Now, you need to write four functions as follows:
formatDateAmerican
takes as input three integer parameter, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format m/d/yyyy
returns nothing
formatDateEuropean
takes as input three integer parameters, one for the year, one for the month and one for the day
prints a formatted version of the date to the console, using the format d.m.yyyy
returns nothing
formatTimeAmerican
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the format H:MMam or H:MMpm
returns nothing
formatTimeEuropean
takes as input two integer parameters, one for the hour, one for the minutes, and a string parameter that contains either "morning" or "evening"
prints a formatted version of the time to the console, using the 24 hour clock. Note that there is no am or pm in this format.
returns nothing

  • Upload your dateTime.cpp file to Catalyst.

Your output should look identical the output below when you are finished:





Upcoming Assignments
  • Assignment 16 due Tuesday
  • Midterm 2 next class
  • Lab 7 due Friday at midnight