Welcome to Lesson 15!


Learning Objectives
By the end of today's class, you should know...
  • How do you write a simple function?
  • What are the different parts of the function signature and what is the purpose of each one?
    • name of function
    • return type
    • parameter list
  • How do you return a value from a function?
  • What is a local vs global variable?
  • How do you "call" a function?

Announcements

  • Midterm on Thursday
    • Just like the previous midterm in format (~15 multiple choice, short answer, T/F + one full program)
    • Will cover all material since last midterm up to today's review activity.
    • What to study:
      •  Your old quizzes
      • In-class review exercises
      • Programs you have written in class and for homework.
    • Article and Ted talk on successful students:
      • Successful students don't work harder and aren't smarter
      • Instead, they study more effectively
      • They give themselves practice quizzes
      • They space out studying over time instead of cramming at the last minute
        • Include breaks and fun things in their study schedule
      • They don't try to memorize by doing things like re-copying notes or re-reading notes
      • Instead, they try to understand, not just memorize!
  • Quiz 5 returned after the break
  • Last day to drop with a "W" is this Friday, November 18
    • See tonight's homework to calculate your current grade

Review Activity

With a partner, answer the following questions. Note that these are the types of questions I will ask you on your midterm!

  • Write a for loop to print the following string out character by character:
string BB= "Basketball";
  • Write one line of code to read user input into a string name sentence:
       cout << "Please enter a sentence: ";
    string sentence;
    //write your line here
  • Fill in the missing lines of code below to handle the case where a user enters a string instead of the expected numerical input.

double height;

cout << "Please enter your height in inches: ";

cin >> height;

while (???????????????) {

    cin.clear();

    ??????

    cout << "Please enter numbers not words.\n";

    cout << "Please enter your height in inches: ";

    cin >> height;

}


Strings Continued

The Problem with Newlines

  • When you press the Enter key, a newline character ('\n') is inserted as part of the input
  • The newline character can cause problems when you mix cin >> with getline()
  • Recall that cin >> s1:
    1. Skips whitespace
    2. Reads non-whitespace characters into the variable
    3. Stops reading when whitespace is found
  • Since whitespace includes newline characters, using cin >> will leave a newline character in the input stream
  • However, getline() just stops reading when it first finds a newline character
  • This can lead to mysterious results in code like the following:
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << "Enter your full name: ";
    string name;
    getline(cin, name);
    cout << "Your age: " << age << endl
         << "Your full name: " << name << endl;
    
  • To correct this problem we use cin >> ws just before getline()
    cin >> ws; // clear whitespace from input stream
    
  • We can see how to use this fix in the following example

Example Using cin >> ws


#include <iostream>
using namespace std;

int main() {
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << "Enter your full name: ";
    string name;
    cin >> ws; // clear whitespace from buffer
    getline(cin, name);
    cout << "Your age: " << age << endl
         << "Your full name: " << name << endl;
}

Activity 15.1: Sentence Continued (10 pts)
  • Let's add to our program involving sentences from last class. We will also calculate the number of letters in the sentence and take in a user guess for a number of letters.
  • At the end of the program, we will confirm whether their guess was correct or not.
  • Declare a new variable beneath the other two at the top of the program. This variable will be used to store our calculation for the number of letters. 
  • Also, add a variable to store the user input for their guess. 
  • The variable declaration section of your program should now look like this:
string sentence;
int numWords = 1;
int numLetters = 0;
int guess;
  • Now alter your first cout statement to reflect the additional uses of this program. Remove the original message in your cout statement and replace it with the one below:
Think of a sentence in your mind.
Later I will tell you how many words and letters are in your sentence.

  • Now, ask the user to enter a guess for how many letters are in the sentence. We want the user to guess without counting the number of letters.
Enter a guess for the number of letters in your sentence (don't count!): _
  • Store the user guess as the guess variable using cin.
  • Next, prompt the user to enter the sentence. Your prompt should remain the same from the last exercise and, as before, you should use getline() to store the user input as the sentence variable.
Please enter your sentence: _
  • Verify that your code inside main looks like identical to the code below:
  • Now, run your program. You should notice a problem.
  • How can we fix this problem?
  • Add a cin >> ws above your getline(cin, sentence);
cin >> ws;
getline(cin, sentence);
  • Compile and run your code again and verify that it is now working properly.
  • Now let's alter the code inside the for loop to calculate how many letters are in the sentence.
  • Since we don't want to count any blank spaces, we only want to increment the numLetters variable when we are NOT incrementing the numWords variable. 
  • Therefore, we need to add an else statement to our for loop. Make sure your if-else in the for loop looks like this:
if (sentence[i] == ' ') {
    numWords++;
} else {
    numLetters++;
}
  • Now, let's add another cout statement below the for loop to print out the number of letters in the sentence,
cout << "And, " << numLetters << " letters.\n";
  • Did our user guess the number of letters correctly? Now is the time to let him or her know. Add the following if-else block above the return 0; of main.
if (guess == numLetters) {
    cout << "You guessed right!\n";
} else {
    cout << "You guessed wrong!\n";
}
  • Run your program again and you should get the following output. Note: if you don't get the output below, compare your program to the final version at the end of this exercise. When you are finished, upload to Catalyst.
  • Your final code should look identical to the following:


Introducing Functions

C++  comes with libraries of predefined functions

  • Example:  sqrt function returns, or computes, the square root of a number:
double the_root = sqrt(9.0);

 

  • The number, 9.0, is called the argument
  • the_root will contain 3.0

Function Calls

  • sqrt(9.0) is a "function call."
  • It invokes, or sets in action, the sqrt function
  • The argument (9), can also be a variable or an expression
  • A function call can be used like any expression:
double bonus =  sqrt(sales) / 10;

cout << "The side of a square with area " << area
         << “ is “
         << sqrt(area);


What functions have we seen so far in this class?

  • pow(base, exponent)
  • sqrt(number)
  • length()
  • substr(index, numChars)
  • getline(cin, stringVariable)
  • setprecision(n)
  • Any others?
  • What do they all have in common? Notice any similarities in their syntax?

Function Call Syntax

  • Function_name (Argument_List)
  • Argument_List is a comma separated list:  (Argument_1, Argument_2, … , Argument_Last)   

side = sqrt(area);
cout << "2.5 to the power 3.0 is "
         << pow(2.5, 3.0);

Libraries

  • Predefined functions are found in libraries
  • The library must be “included” in  a program to make the functions available
  • An include directive tells the compiler which library header file to include.
  • To include the math library containing sqrt():
        #include <cmath>
  • Newer standard libraries, such as cmath, also require the directive
using namespace std;

Other Pre-Defined Functions
  • abs(x)     ---  int value = abs(-8);
    • Returns absolute value of argument x
    • Return value is of type int
    • Argument is of type x
    • Found in the library cstdlib
  • fabs(x)     ---  double value = fabs(-8.0);
    • Returns the absolute value of argument x
    • Return value is of type double
    • Argument is of type double
    • Found in the library cmath

Writing Our Own Functions

Grouping Repeated Commands

  • Some of the main() functions in our programs have been getting lengthy and complicated
  • The biggest problem in developing software is managing the complexity of programs
  • We can improve our code by organizing it into smaller pieces known as functions
  • Functions are a key tool in creating easy-to-understand programs that can be changed easily

Video: Chris Bosh Explains Functions

  • After watching the video, what does it mean to write our own functions?

What is a Function?

  • As developers, we need to know how to write and call functions

    Function: a named block of statements that can receive input, perform an action, and optionally return a value

  • Functions are like little programs in our larger program
  • We give each little function commands we want executed
  • We call the function whenever we want the commands executed
  • When the function has finished running, program execution returns to the point just after the code that called the function

Example Application for a Function

  • As an example, recall our test code to validate user input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main() {
    double input = 0.0;
    do {
        cout << "Enter a positive number: ";
        cin >> input;
        if (cin.fail()) {
            cout << "You must enter digits, not words\n";
            cin.clear();
            cin.ignore(1000, '\n');
            input = -1; // set loop test to fail
        } else if (input <= 0.0) {
            cout << "You must enter a positive number\n";
        }
    } while (input <= 0.0);
    cout << "You entered: " << input << endl;

    return 0;
}
  • What if we need to enter two validated numbers into a program?
  • We want to process the first number after input and then input the second number
  • Doing so, we would end up with code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

int main() {
    double input = 0.0;
    do {
        cout << "Enter a positive number: ";
        cin >> input;
        if (cin.fail()) {
            cout << "You must enter digits, not words\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
            input = -1; // set loop test to fail
        } else if (input <= 0.0) {
            cout << "You must enter a positive number\n";
        }
    } while (input <= 0.0);

    // Process the input
    cout << "You entered: " << input << endl;

    double input2 = 0.0;
    do {
        cout << "Enter a positive number: ";
        cin >> input2;
        if (cin.fail()) {
            cout << "You must enter digits, not words\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
            input2 = -1; // set loop test to fail
        } else if (input <= 0.0) {
            cout << "You must enter a positive number\n";
        }
    } while (input <= 0.0);

    // Process the second input
    cout << "You entered: " << input2 << endl;

    return 0;
}
  • Our program would be easier to write if we could get the second input without repeating the code
  • With functions, we give the list of commands a name and then run the list by calling the name
  • Using functions we keep all the code in one place and avoid duplication
  • Avoiding duplication reduces the complexity of our code and makes it easier to understand and change

Programming Style: Avoid Duplicating Code

  • Duplicate code can lead to problems such as:
    • Long repeated sections that are more difficult to understand than shorter sequences
    • Repetition of largely identical code within which it is difficult to see the different purposes of each section
    • Update problems where we make changes in some sections but overlook making changes in other sections
  • If we find ourselves writing similar code of three or more lines multiple times, we should consider writing a function

Defining a Function

  • In this section we look at function definition syntax and examine a simple example function
  • After we understand the syntax we can write more complicated functions

Function Syntax

  • The general syntax for defining a function is:
    returnType functionName(parameter1, ..., parametern) {
        statements
    }
    
  • Where:
    • returnType: the data type of the value returned
    • functionName: the name you make up for the function
    • parameterx: the input values, if any
    • statements: the list of statements to execute when the function is called
  • Can you identify each of these syntax items in the function we have always used?
    int main() {
        // program statements go here
    }
    

Example Program with a Second Function

  • As an example, the following program has a simple function to add two numbers
  • Notice that the code has two functions: add() and main()
  • The second function is placed before main() so the compiler knows about the function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int add(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    cout << "Enter two numbers to add: ";
    int num1, num2;
    cin >> num1 >> num2;
    int total = add(num1, num2);
    cout << "Sum=" << total << endl;

    return 0;
}

Function Name

  • Every function must have a name that identifies the function
  • Function names follow the same rules as variable names
  • Technically, we can use any valid identifier for a function name
  • However, we should use a name that suggests the action the function performs
  • In our example, add suggests that the function will return the sum of two numbers

Function Structure

  • The first line of a function is known as the function signature
    int add(int a, int b)
    
  • The curly braces {...} contain the function body
  • The function body is the list of statement the function executes when called
  • The function signature describes the name, inputs and output of a function
  • We will look at these features in more detail in the following sections

Parameters

  • When defining a function, it is worth thinking about what helpful action it will perform
  • We can make the function more useful if we give it parameters
  • The parameters for an add function would be the two numbers to sum
  • Read through the following code to identify how the code makes use of the parameters

Example Code with Function Parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int add(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    cout << "Enter two numbers to add: ";
    int num1, num2;
    cin >> num1 >> num2;
    int total = add(num1, num2);
    cout << "Sum=" << total << endl;

    return 0;
}

Parameter List

  • We must have parenthesis after a function name
  • Inside the parenthesis, we define a list of zero or more parameters
  • Parameters are the inputs to a function
  • In our example, we have two parameters inside the parenthesis
    int add(int a, int b)
    
  • Parameters are the declaration of a new variable, even though they are declared inside parenthesis
  • Each parameter must have both a type and a name, just like a regular variable
  • If we have more than one parameter, we separate them with commas
  • Any parameter that we declare must be given an argument when we call the function
  • In the following image, the value of arguments num1 and num2 are copied to the parameters a and b

Passing Arguments to Function Parameters


Arguments and Parameters

  • Depending on our background, we might use the term arguments or parameters for the values passed to functions
  • The terminology is not that important
  • However, the way I will use the terms is:
    • A function definition has parameters
      int add(int a, int b) { // a and b are parameters
          // ...
      }
      
    • A function call passes arguments
      add(num1, num2); // num1 and num2 are arguments
      
  • Arguments are values we pass into functions
  • When the argument drops into a function, it lands in a parameter
  • A parameter is just like other variables in the function
    • Except that a parameter gets initialized by an argument
  • The important part is:

    We must pass every function parameter an argument.

  • The arguments must be in the same order as the parameters
  • Also, the argument value must be compatible with the type of the parameter
  • For example, we cannot call add() with: add("Jennifer", "Parrish")



Returning a Value

  • The first word in the function signature is the return type
    int add(int a, int b)
    
  • The return type specifies the type of data the function outputs
  • In our example the return type is an int

Return Statement

  • Functions that return a value must execute a return statement
    return result;
    
  • For instance, our example function add() has a return statement
    int add(int a, int b) {
        int sum = a + b;
        return sum;
    }
    
  • Note that the type of the returned valued must be compatible with the function return type
  • The returned value is substituted for the function call in the calling code
    sum =>[replaces]=> add(num1, num2)
    
  • We must save the returned value if we want to process it later in the program
    int total = add(num1, num2);
    

Returning a Value from a Function


Returning an Expression

  • The value after the word return can be an expression
  • It does not have to be just the name of a variable
  • We could rewrite our return statement to the following:
    return a + b;
    

Multiple return Statments

  • We can have more than one return stattement in a function
  • The first return statement reached is the one executed
  • We might have multiple returns if we have if-statments with alternate actions, like:
    if (x > 400) {
        return 1;
    } else {
        return 0;
    }
    
  • We do not have alternate actions in our simple add function and so have only one return statement

Activity 15.2 Writing a Function (10 pts)

In this exercise we define our own function.

Specifications

  1. Copy the following program into a text editor, save it as sub.cpp, and then compile and run the starter program to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    // Define function here
    
    int main() {
    
        return 0;
    }
    
  2. Write the signature for a function named sub that receives two int numbers and returns an int value, like we did for the add() function
    returnType sub(two_int_parameters)
    
  3. Add the curly braces for the function body: { }.
  4. Inside the function body, subtract the second parameter from the first and return the value, like we did for the add() function.
        int sum = a + b; // from add() function, CHANGE THIS!
        return sum;
    
  5. Compile and run your code. What do you see when you compile? (click here) Click to show answer
  6. Inside the main() function, enter these statements:
        cout << "Enter two numbers to subtract: ";
        int num1, num2;
        cin >> num1 >> num2;
        int diff = sub(num1, num2);
        cout << "Difference=" << diff << endl;
    

    The fourth line contains the function call.

  7. Compile and run your modified program and verify the output looks like:
    Enter two numbers to subtract: 3 1
    Difference=2
    
  8. Save your sub.cpp file and submit it to Catalyst.

Completed Program

When finished, your application should look like the following.


Function Analogies

  • Functions as a black box.
  • Functions as a blue print.

  • Functions as outsourcing or multiple departments within a company.

  • Functions as a juicer.





Wrap Up
  • With a partner, answer the questions from today's learning objectives


Upcoming Assignments
  • Assignment 15 due Tuesday at 3:20pm on Catalyst
  • Midterm 2 next class
  • Lab 7 due Friday at midnight