Schedule‎ > ‎

Lesson 7: Getting Functional

Contents

  1. 1 Continuations
    1. 1.1 NASA Swarmathon
  2. 2 7.1: Predefined Functions
    1. 2.1 Objectives
    2. 2.2 7.1.1: Introducing Functions
    3. 2.3 7.1.2: Calling a Function
    4. 2.4 7.1.3: Generating Random Numbers
    5. 2.5 7.1.4: Seeding the Random Generator
    6. 2.6 Exercise 7.1: Getting Random (8m)
    7. 2.7 7.1.5: Simulating Dice*
    8. 2.8 7.1.6: Summary
  3. 3 7.2: Coding Functions
    1. 3.1 Learner Outcomes
    2. 3.2 7.2.1: Grouping Repeated Commands
    3. 3.3 7.2.2: Defining a Function
    4. 3.4 7.2.3: Parameters
    5. 3.5 7.2.4: Variable and Parameter Scope
    6. 3.6 7.2.5: Returning a Value
    7. 3.7 Exercise 7.2a: Writing a Function (8m)
    8. 3.8 7.2.6: Flow of Control for a Function Call
    9. 3.9 Exercise 7.2b: Tracing a Function Call (5m)
    10. 3.10 7.2.7: Some Style Requirements for Functions
    11. 3.11 7.2.8: Summary
  4. 4 7.3: Midterm Preparation
    1. 4.1 Learner Outcomes
    2. 4.2 7.3.1: About the Exam
    3. 4.3 7.3.2 Suggested Study Order (yes, this will take more than a few hours)
    4. 4.4 1.  Review Quiz 1 & Quiz 2.  Make sure you can answer all the questions.2.  Review the Test Topics list below and make sure you can write code snippets for each topic.3.  Finish the Assignment 6 Midterm 1 Prep4.  Review the code in each Assignment.  Completed code for each Assignment is now at the bottom of each Assignment.5.  Review the code in KEY SAMPLE CODE at the top of the Schedule Page.
    5. 4.5 Form a Study Group with 1 or more students in the classArrange to meet for at least 1 hour to study for Midterm 1.  Set a time, day and place (STEM center?)Best study groups focus on Midterm prep and minimize other distractions. Help each other succeed.  Write your groups names, time, day and place on a sheet of paper and hand it in to your instructor for credit.
    6. 4.6 Exercise 7.3: Preparing Exam Questions (3m)




Continuations

NASA Swarmathon

  • Cabrillo Robotics Club participating
  • You can join the Robotics Club
  • Meetings tend to be in room 829 at noon on Fridays
  • Opportunity to improve your programming skills


7.1: Predefined Functions

Objectives

At the end of the lesson the student will be able to:

  • Create code that calls predefined functions
  • Describe the flow of control of a function call
  • Write code that generates random numbers


7.1.1: Introducing Functions

Function -- a subprogram (method, procedure or subroutine) that executes a block of code when called.

  • Most popular programs are large, containing thousands to millions of lines of code
  • When programs are this large, the best way to develop and maintain them is to construct them from smaller pieces or modules
  • In C++, these smaller pieces are called functions
  • The name function comes from mathematics where we write generalized formulas like:

    f(x) = 2x + 3

  • With the above formula, we can plug in a value for x and get a final value
  • In C++ we would write code like the following for the above math function:
    int f(int x)
    {
        return 2 * x + 3;
    }
    
  • Notice that we have been writing functions named main() for every program:
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello, World!\n";
        return 0;
    }
    

Function Libraries

  • Function are often collected into libraries so they can be reused by many different programs
  • C++ comes with many common libraries of functions such as <cmath> which contains sqrt()
  • We have used many of these library functions and will use more as the course progresses
  • Functions such as these are known as predefined functions
  • A predefined functions is simply a function that someone else wrote
  • Our use of predefined functions shows one of the benefits of functions: reusable code
  • Later on we will learn to define our own functions so that we can create reusable code as well
  • For now, we are going to focus on how to use functions

Library Functions

  • Here are some library functions we have used so far:
    exp(exponent);
    pow(base, exponent);
    sqrt(number);
    length();
    substr(index, numChars);
    getline(cin, stringVariable);
    
  • Any others?
  • What do they all have in common? Notice any similarities?

Check Yourself

  1. A named block of code that executes its statements when called is known as a(n) ________.
  2. Many predefined functions are available in program ________.
  3. Function that are previously written and included in the C++ libraries are known as ________ functions.


7.1.2: Calling a Function

  • When a function is defined or included in a program, C++ ignores the statements at first
  • In order to execute the statements, we must call the function

    Function call: a request by a program to execute a function.

  • A function call passes control to the called function
  • After completing the function, control returns to the calling function (like main())
  • Calling a function is like a boss asking a worker to do something
  • The "boss" (calling function) asks the "worker" (called function) to complete a task

Flow of Control for a Function Call

  • To understand how a function works, we need to trace its flow of control
  • The programs we write in this course have a single flow of control
  • All the statements in our programs execute in sequence, which can be modified by conditional statements or loops
  • A function call is another way to change the flow of control
  • When we call the function, the calling function stops and waits for the called function to finish
  • We can see this flow in the following diagram:
    Flow of Control for a Function Call

Arguments and Returned Values

  • When we call a function we can send the function data called arguments
  • For example, to calculate the square root of a number, we call the sqrt() function with a particular argument like 9.0:
    sqrt(9.0);
  • In this case the argument is the numerical value 9.0
  • When the called function finishes executing, it returns to where it was called from
  • When the function returns, it may return a value
  • In the following example, our function call returns the value 3.0
  • We combine the returned value with an arithmetic expression and then save the expression in a variable

Example Program with a Function Call

1
2
3
4
5
6
7
8
9
10
#include <cmath>
#include <iostream>
using namespace std;

int main() {
    double result = 1 + sqrt(9.0);
    cout << result << endl;

    return 0;
}

Check Yourself

  1. True or false: functions are executed as soon as the compiler reaches the function code.
  2. To execute the code inside of a function we must write a function ________.
  3. True or false: function calls always include parenthesis.
  4. After a function ends, control returns to the ________ statement.
  5. data passed to a function is known as an ________.


7.1.3: Generating Random Numbers

  • As an example of predefined functions, let us look at random numbers
  • Random numbers are a series of numbers whose future values cannot be predicted
  • Many computational problems need to use random numbers like:
    • Cryptography in many areas including generating passwords
    • Simulation of unpredictable real phenomena
    • Random numbers in games of chance like dice rolling
    • Random shuffling of the audio files in an audio player
  • The C++ library has a random number generator, rand(), that produces a series of psuedorandom numbers:
    • Range is 0 up to and including RAND_MAX
    • Returns an "random" int value
  • To make use of the rand() function we may need to include the library:
    #include <cstdlib>
    
  • The syntax for calling the function is:
    rand()
    
  • For example:
    int randNumber = rand(); // returns number between 0 and RAND_MAX
    

Psuedorandom

  • The numbers produced by the library appear to be random but are not
  • We call these types of random numbers psuedorandom
  • Psuedorandom means that given an initial starting condition, the procedure always produces the same result
  • Random numbers actual come from a a very long sequence of numbers computed from fairly simple formulas; they just behave like random numbers
  • The following program uses rand() to produce the same output every time it runs
  • The reason that the numbers are the same is because the numbers are generated with the same computer commands every time it is called
  • When running the following program, remember the first few numbers and then start the program again
  • Notice that the same sequence of numbers is produced

Example Program Using rand()

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

int main() {
    int i;
    for (i = 0; i < 10; i++) {
        int num = rand();
        cout << num << "\n";
    }

    return 0;
}

More information

Check Yourself

  1. A number whose value cannot be predicted ahead of time is known as a ________ number.
  2. The problem with this random number generator from xkcd is that ________.
    1. dice rolling is not a fair way to get random numbers
    2. it was not selected randomly
    3. you can predict the number ahead of time
    4. nothing is wrong
  3. The function rand() returns a number between ________ and including ________.


7.1.4: Seeding the Random Generator

  • When running programs we do not always want the same sequence of numbers every time
  • For instance, if we run a dice simulation for a game we want different numbers every time the program runs
  • To get a different number, we must "seed" the random number generator
  • We set the seed with the srand() function with the syntax:
    srand(seed)
    
  • Where:
    • seed: the initial number for the random sequence
  • If we change the seed value, we will get different numbers every time

Changing the Starting Value

  • One common strategy for changing the seed value is to use the current time
  • Since the time changes every second, we get a new random number sequence every time we run our program
  • To generate a number from time, we can use the time() function from the standard time library:
    srand(time(0));
  • The expression time(0) returns the number of seconds since January 1, 1970
  • To make use of the time() function we may need to include the library:
    #include <cstdlib>
    
  • We only call srand(time(0)) once in a program or we may get repeated numbers

Example Program Using srand()

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

int main() {
    srand(time(0));
    int i;
    for (i = 0; i < 10; i++) {
        int num = rand();
        cout << num << "\n";
    }

    return 0;
}

Check Yourself

  1. The function that sets the starting location, or seed, for rand() is ________.
  2. To produce a new random number sequence every time a program starts use the code: ________.
  3. True or false: you must reseed the random number generator every time before calling rand().


Exercise 7.1: Getting Random (8m)

In this exercise we look at how to call functions and learn how to generate random numbers.

Specifications

  1. Copy the following program into a text editor, save it as dice.cpp, and then compile the starter code to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Enter your code here
    
        return 0;
    }
    
  2. Add the library cstdlib at the top of the file:
    #include <cstdlib>
    
  3. Random numbers are generated by using the rand() function. Declare two integer variables, die1 and die2, and assign them values returned by the rand() function using code like the following:
    int die1 = rand();
    int die2 = rand();
    cout << "You rolled a " << die1
         << " and a " << die2 << endl;
    

    For more information, see section: 7.1.3: Generating Random Numbers.

  4. Compile and run your program and make sure your output looks like the following. Note that the actual numbers may be different.
    You rolled a 0 and a 1481765933
    

    Notice the size of the numbers displayed. We want to limit the size of the random numbers generated by rand() to only six numbers. We limit the range by using the modulus (%) operator.

  5. Change the two assignment statements with the following code:
    int die1 = rand() % 6;
    int die2 = rand() % 6;
    

    The number 6 in the above code is known as the scaling factor since it limits the scale of the numbers produced by the rand() function.

  6. Compile and run your program and make sure your output looks like the following. Note that the actual numbers may be different.
    You rolled a 0 and a 5
    
  7. Seeing just one roll of the dice is not very useful. Add a counting loop to roll and display the dice 10 times. For more information, see section: 5.1.1: Using Loops to Count. You may want to use a for loop for counting as described in section 5.1.2: for Statements.
  8. Compile and run your program and make sure your output looks like the following. Note that the actual numbers may be different.
    You rolled a 0 and a 5
    You rolled a 3 and a 2
    You rolled a 5 and a 0
    You rolled a 0 and a 4
    You rolled a 0 and a 2
    You rolled a 1 and a 3
    You rolled a 5 and a 3
    You rolled a 5 and a 0
    You rolled a 5 and a 0
    You rolled a 0 and a 5
    

    Notice that we get numbers in the range of 0 to 5 when we want 1 to 6. To correct this problem, we must add one to each of the statements generating the random numbers. Go ahead and make this change now. For more information, see section: 7.1.5: Simulating Dice.

  9. Rerun your code two or more times and check the numbers rolled. Do you see any patterns? To correct the problem we must "seed" the random number generator. Add the following code after the start of main and before your counting loop:
    srand(time(0));
    

    For more information, see section: 7.1.4: Seeding the Random Generator.

  10. Compile and run your program and make sure your output looks like the following. Note that the actual numbers may be different.
    You rolled a 1 and a 1
    You rolled a 4 and a 5
    You rolled a 5 and a 6
    You rolled a 3 and a 6
    You rolled a 2 and a 4
    You rolled a 4 and a 6
    You rolled a 3 and a 4
    You rolled a 1 and a 5
    You rolled a 4 and a 5
    You rolled a 4 and a 6
    
  11. Submit your dice.cpp to Canvas Ex7.1

Read the following section and be prepared to answer the Check Yourself questions.


7.1.5: Simulating Dice*

  • simulation is an imitation of some real thing or process
  • We create a simulation of randomness in a computer program by generating random numbers
  • One simple random simulation we can do is a pair of dice

Setting the Range

  • We need a number between 1 and 6, but rand() returns a number between 0 and RAND_MAX
  • We need only random numbers for a die and so we scale the range of RAND_MAX using the % operator
  • Now we can generate random integer numbers between 0 and 5
  • To get a number between 1 and 6, we shift the numbers by adding 1
  • Our code to simulate a single die with random numbers is now:
    int die = 1 + rand() % 6;
    cout << die << endl;
    
  • We can generalize our formula for producing integer random numbers to:
    rand() % SCALING_FACTOR + SHIFTING_VALUE;
    
  • The following program implements our dice rolling simulation
  • Note how the program simulates rolling 2 dice using 1 die at a time
  • You would get a different result if you just generated a random number between 2 and 12
  • To repeat the dice rolling, we use a loop

Example Program Simulating the Rolling of a Pair of Dice

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

int main() {
    srand(time(0));

    for (int i = 0; i < 10; i++) {
        int die1 = rand() % 6 + 1;
        int die2 = rand() % 6 + 1;
        cout << "You rolled a " << die1
             << " and a " << die2 << endl;
    }

    return 0;
}

Check Yourself

What code do you write to get the following integer random numbers, including the end points?

  1. A random number between 0 and 5. 
  2. A random number between 1 and 6. 
  3. A random number between 1 and 10. 
  4. A random number between -10 and 10.


7.1.6: Summary

  • A function is a block of code gets executed when called
  • During a function call, control is passed to the called function
  • When the called function finishes executing, control returns to the calling function and statement
  • C++ has many predefined functions in several libraries
  • Libraries must be "included" in a program:
    #include <cmath>
  • Newer standard libraries, such as cmath, also require the directive:
    using namespace std;
  • One of the library functions generate "random" numbers: rand()
  • To seed the random number generator, you use the srand() function
  • We can use these functions to simulate the rolling of a die:
    srand(time(0));
    
    int die = 1 + rand() % 6;
    cout << die << endl;
    
  • You can use random numbers for other computations as well, such as x and y coordinates:
    double r = (double) rand() / ((double) RAND_MAX + 1.0);
    double y = r * 20 - 10;
    

Check Yourself

Answer these questions to check your understanding. You can find more information by following the links after the question.

  1. What is a function? (7.1.1)
  2. Why do we use functions? (7.1.2)
  3. Where does the control flow during a function call? (7.1.2)
  4. What is an argument? (7.1.2)
  5. What is a returned value? (7.1.2)
  6. What is a random number? (7.1.3)
  7. What is the value of RAND_MAX on our classroom computers? (7.1.3)
  8. How do you make the rand() function return a different random sequence when you run your program again? (7.1.4)
  9. How do you code a random number that simulates rolling a die? (7.1.5)
  10. What statement do we use to limit the range of the values returned by the rand() function to the values 1 through 10? (7.1.5)


7.2: Coding Functions

Learner Outcomes

At the end of the lesson the student will be able to:

  • Discuss the reason for writing functions
  • Define functions with and without parameters
  • Pass arguments to functions
  • Return values from functions
  • Trace the flow of a function call


7.2.1: 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

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(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);

    // 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(1000, '\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

Check Yourself

  1. True or false: people write functions to organize code into small understandable pieces.
  2. Which of the following are features of a function?
    1. Must have a name
    2. Can receive input
    3. Performs an action
    4. Must return a value
  3. True or false: when a function finishes executing, the program flow returns to the point just after the code that called the function.
  4. True or false: functions can reduce the amount of repeated code, making programs shorter.


7.2.2: 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

Check Yourself

  1. True or false: function names are case sensitive.
  2. Of the following, ________ would be a valid function definition.
    1. fun() { /* C++ statements */ }
    2. int fun;
    3. int fun() { /* C++ statements */ }
    4. int fun();
  3. The statements inside a function are surrounded by ________.
    1. Curly braces -- { }
    2. Parenthesis -- ( )
    3. Square brackets -- [ ]
    4. Colons -- : :
  4. Which of the following is a function definition and which is a function call?
    1. int drawSquare(int x, int y) { /* statements */ }
    2. drawSquare(4, 2);


7.2.3: 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
  • Recall the use of functions in mathematics like:

    f(x, y) = x + y

  • With the above formula, we can plug in a value for x and y to get a final value
  • Like in math, we need two parameters for an add function
  • 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
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("Ed", "Parrish")

Check Yourself

  1. To received input, a function has __________.
  2. Parameters are set on the first line of a function inside a set of __________
    1. Curly braces -- { }
    2. Parenthesis -- ( )
    3. Square brackets -- [ ]
    4. Colons -- : :
  3. True or false: like any other variable, a function parameter has both a type and a name.
  4. If a function has three parameters, a function call must include ________ arguments.
  5. For the following function signature, the parameter names are __________
    int add(int a, int b)
    
    1. a
    2. b
    3. both a and b
    4. cannot tell from the function signature
  6. The following code snippet prints ________.
    int addTwo(int x) {
      return x + 2;
    }
    int main() {
      int x = addTwo(40);
      cout << x << endl;
      return 0;
    }
    


7.2.4: Variable and Parameter Scope

  • A variable declared inside a function can only be used within that function

    Local variable: a variable that can only be accessed within a function or block.

  • Parameters are a local variable and thus can only be used inside the function in which they are declared as well
  • As an example of a local variable, we declared sum inside the add() function:
    int sum = a + b;
    
  • In addition, we declared another variable named total inside main():
    int total = add(num1, num2);
    
  • These variables cannot be accessed outside the function they were declared within

Scope

  • The area of code that a variable can operate within is known as it's scope

    Scope: the enclosing area within which a variable exists

  • Because of scope, we can use variables with the same name in different functions
  • To send information to a function we must include a parameters:
    int add(int a, int b)
    
  • When the function call is made, we send the arguments to the parameters:
    add(num1, num2)
    
  • The values of num1 and num2 are copied to the parameter variables a and b

Example of Scope

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 display(int a) {
    cout << a << endl; // What will this print?
    int x = 42;
    cout << x << endl; // What will this print?

    return 0;
}

int main() {
    int x = 7;
    cout << x << endl; // prints 7
    x = 11;
    cout << x << endl; // prints 11
    display(x);
    cout << x << endl; // What will this print?

    return 0;
}

Check Yourself

  1. A variable that can be used only within the function in which it was declared is known as a __________ variable.
  2. True or false: parameters are local variables.
  3. The area of a program within which a variable exists is known as its ________.
  4. The following code snippet prints ________.
    int myFun(int a) {
      int x = 12;
      a = x;
      a = a + 3;
      return 0;
    }
    int main() {
      int x = 42;
      myFun(x);
      cout << x << endl;
    
      return 0;
    }
    
    1. 12
    2. 15
    3. 42
    4. 45


7.2.5: 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 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

Check yourself

  1. To return a value from a function use a __________ statement.
  2. True or false: a value returned from a function must be compatible with the function return type.
  3. To correctly assign the result of a function named getSomething() to a variable named shopping, use __________.
    1. shopping = getSomething();
    2. shopping = getSomething;
    3. shopping(getSomething);
    4. shopping() = getSomething();
  4. The following code snippet prints ________.
    int get42() {
      return 42;
    }
    
    int main() {
      int x = get42() + 2;
      cout << x << endl;
      return 0;
    }
    


Exercise 7.2a: Writing a Function (8m)

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. For more information on function calls, see section: 7.3.2: Calling a Function.

  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 to Canvas Ex 7.2 with your file from the next exercise 7.2b.

When finished, please help those around you.


7.2.6: Flow of Control for a Function Call

  • To use functions well, we must understand the flow of a program when calling functions
  • Every program starts executing in the main() function
  • When a program gets to a statement like the following, it stops executing in main() and jumps to our function:
    int total = add(num1, num2);
    
  • The program executes the statements in the function and then returns to the point in the code right after where it jumped
  • When the function returns, the returned value replaces the function call
  • After returning, the program completes processing the calling statement and then moves on to the next statement
  • In our example, the statement saves the returned value in the variable: total
  • Every time the flow of control reaches a function call, the program:
    1. Temporarily stops executing in the current function
    2. Jumps to the called function and executes the statements of that function
    3. Returns to the point in the code just after where it jumped

Function Call Flow
Function Call Flow

  1. Every program starts executing at the start of the function main().
  2. When reaching a function call, arguments are copied to the parameters.
  3. Function code executes until reaching a return statement.
  4. Return statement returns a value to the function call.
  5. Calling function continues after the function returns.

Check Yourself

  1. Every program starts executing at the start of the function __________.
  2. True or false: when reaching a function call, the currently executing function stops and control jumps to the start of the called function.
  3. True or false: the main() function continues executing when a function call returns.
  4. True or false: after a called function returns, control returns to the point in the code just after where it was called.


Exercise 7.2b: Tracing a Function Call (5m)

In this exercise we trace the function we developed in the last exercise.

Specifications

  1. Create a text file named trace.txt.
  2. In the trace.txt file, list the line numbers of each statement of your program from the last exercise in the order the lines are executed. For example, if main() starts on line 9, statements are executed as follows:
    9, 10, 11, 12, ...
    

    Do not bother to list blank lines or lines containing only a curly brace (}) of a function definition.

  3. Review the hand trace with another student in the class. Then add a comment to the top of the file that contains the name of the person with whom you reviewed the code, like:
    Reviewed trace with Fred George.
  4. Save the trace.txt and submit to Canvas Ex 7.2 with your file from the previous exercise.

As time permits, read the following sections and be prepared to answer the Check Yourself questions in the section: 7.2.7: Summary.


7.2.7: Some Style Requirements for Functions

  • Consider again our example 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;
}
  • Note the placement of the curly braces
  • There are two common styles of curly brace placement for functions:
    1. Place the opening brace on the same line as the function heading:
      int myFunction() {
          // statements of the function
      }
      
    2. Place the opening brace under and lined up with the first letter of the return type:
      int myFunction()
      {
          // statements of the function
      }
      
  • We can use either style as long as we are consistent
  • Also notice the indentation of the statements inside the function
  • As before, we always indent 3-4 more spaces after an opening curly brace
  • After the closing curly brace, we no longer indent the extra 3-4 spaces
  • Indenting makes it easier to see the block of code
  • In addition, function names always start with a lower case letter like variables
  • We can tell the difference between function and variable name because functions have parenthesis

Check Yourself

  1. True or false: always indent code within a function's curly braces.
  2. The number of spaces to indent is ________.
  3. Function names always start with a(n) ________ letter.
    1. camel case
    2. lowercase
    3. mixed case
    4. uppercase


7.2.8: Summary

  • As we add more code to main(), it becomes too long to easily understand
  • The solution is to break up the long sequences of code into shorter sections using functions

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

  • Creating functions is like adding new commands to the programming language
  • Functions allow us to organize code into short reusable pieces
  • We then assemble the parts to create larger programs

Defining Functions

  • To define a function, we use the following syntax:
    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
  • As an example, we wrote the function add() which is called from main():
    int add(int a, int b) {
        int sum = a + b;
        return sum;
    }
    int main() {
        //... other code omitted
        int total = add(num1, num2);
        //... other code omitted
    }
    

Parameters

  • In the parenthesis of the function are the parameters
  • Parameters are the inputs to a function
  • When we define a function, we want it to be reusable
  • To make a function more reusable, we avoid hard-wiring important values
  • Instead, we pass the key values by defining parameters
  • When we call the function, we supply an argument for each parameter as shown below

Passing Arguments to Function Parameters

Returning Values


  • 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
  • When we want the function to return a value we write a return statement
    return sum;
    
  • The returned value gets substituted for the function call in the calling code
  • The flow of a function call is shown below

Function Call Flow

Check Yourself

Answer these questions to check your understanding. You can find more information by following the links after the question.

  1. Why do programmers write functions? (7.2.1)
  2. What is a function? (7.2.1)
  3. If you call a function in main(), do you place the function definition before or after main()? (7.2.2)
  4. What are the four main parts of a function? (7.2.2)
  5. Which of the following is a function definition and which is a function call? (7.2.2)
    1. int drawSquare(int x, int y) { }
    2. drawSquare(4, 2);
  6. How can you tell the difference between a function name and a variable name? (7.2.2)
  7. How many parameters can you declare for a function? (7.2.3)
  8. How is a variable declaration different than a parameter? (7.2.3)
  9. Why do functions need parameters? (7.2.3)
  10. If you declare three parameters, how many arguments must you include in a function call? (7.2.3)
  11. What is the difference between an argument and a parameter? (7.2.3)
  12. If you declare a variable inside main() can you access that variable inside another function? (7.2.4)
  13. What statement is used to return values from functions? (7.2.5)
  14. What happens to the flow of control when a function call is reached? (7.2.6)
  15. True or false? You should indent code within a function. (7.2.7)

7.3: Midterm Preparation

Learner Outcomes

At the end of the lesson the student will be able to:

  • Discuss how to prepare for the midterm exam
  • Describe how to take the midterm exam


7.3.1: About the Exam

  • The exam covers material from the first six weeks including arrays.
  • The exam will be on paper.

Ground Rules

  • You must attend the exam or you will receive a score of zero (0)
    • Except by prior arrangement with the instructor
  • The exam is closed books and closed notes
    • However, you may have one 3" x 5" card of handwritten notes for the exam
  • Blank scratch paper is allowed.
  • No electronic devices like cell phones, calculators or music players.
    • If you have a cell phone visible or in use during the exam, you will automatically fail
  • No communication with anyone but the instructor during the exam.

3"x5" Card Requirements

  • Put your name on your card
  • Maximum card (or paper) size is 3 inches by 5 inches
  • You may use both sides of the card
  • Notes must be handwritten and NOT photocopied
  • No more than three statements in a sequence on the card — only snippets
  • Any 3" x 5" cards violating these rules will be confiscated before the test
  • You must turn in your 3" x 5" card stapled to your midterm for 1 pt credit


7.3.2 Suggested Study Order (yes, this will take more than a few hours)

1.  Review Quiz 1 & Quiz 2.  Make sure you can answer all the questions.

2.  Review the Test Topics list below and make sure you can write code snippets for each topic.

3.  Finish the Assignment 6 Midterm 1 Prep

4.  Review the code in each Assignment.  Completed code for each Assignment is now at the bottom of each Assignment.

5.  Review the code in KEY SAMPLE CODE at the top of the Schedule Page.

Exercise 1: Form a Study Group

  1. Form a Study Group with 1 or more students in the class
  2. Arrange to meet for at least 1 hour to study for Midterm 1.  Set a time, day and place (STEM center?)
  3. Best study groups focus on Midterm prep and minimize other distractions. Help each other succeed.  
  4. Write your groups names, time, day and place on a sheet of paper and hand it in to your instructor for credit.


7.3.3: How to Study for the Exam
No one can ace the test without understanding the material and the key to understanding is to study
To get the best grade you need to thoroughly prepare for the test well in advance
With the midterm approaching, you have a choice:
Study and do well, or
Not study and do less than your best
Victims are people who let their lives control them, like a pawn on a chessboard
Creators are people who controls their own life, like a person playing chess
You can take responsibility for studying and do well on a test
Or you can make excuses and do less than you are capable of on a test

Boosting Your Performance

Here are some steps you can take to improve your test results:

  1. Compile a list of topics you might be tested on.

    Look over your assignments, exercises, and lecture notes to determine what you covered. Write a list of topics from these sources.

  2. Make sure you understand all the topics on your list.

    Correct any mistakes you may have made in your assignments or exercises.

  3. Identify the most probable exam topics for thorough study.

    To do well, you need deep understanding of the test topics. Oftentimes, you can tell what is on a test simply by which topics the instructor spends the most time talking about.

  4. Create a list of test questions.

    Turn headings from the textbook and lecture notes into questions. Look at the section summaries in the lectures and read the Check Yourself questions.

  5. Study your questions over and over until you know them perfectly.

    Make flash cards with questions on one side and answers on the other. Carry the cards with you and review them when you have a few minutes available. Have other people ask you questions from your flash cards.

  6. Prepare your 3"x5" card of test notes.

    Since the instructor allows a 3"x5" card, make use of it. For the topics you had the most trouble with, write down a short note or summary. If the pressure of the test causes you to forget something, you can refer to your card.

More Information


Exercise 7.3: Preparing Exam Questions (3m)

Take three minutes to review and choose one of the following topics for A5-Midterm 1 Preparation. Post the questions in the Discussions area of Canvas titled "Midterm 1 Study Questions"

There cannot be more than two posts per topic and all the questions for a post must be unique. Thus if two people select the same topic, the second person who posts on the topic must have different questions than the first person.

Exam Topics

  1. Compiling, errors and debugging: (1.3.5, 2.1.1, 2.4)
  2. Variable declaration: (2.2.2, 3.1)
  3. Variable assignment flow: (3.1.4, Exercise 3.1)
  4. User I/O: (2.2.3, 3.2.4)
  5. Arithmetic: (2.3.1-3, 4.1.1)
  6. Integer division and modulus: (2.3.4)
  7. Math functions: (2.3.5)
  8. Type char and ASCII: (3.2.1)
  9. Type string: (3.2.2-3)
  10. String concatenation: (3.2.5)
  11. String functions: (3.2.6)
  12. if-statements: (3.3.1)
  13. if-else statements: (3.3.4)
  14. Relational operators: (3.3.2-3)
  15. Exponential notation: (4.1.2)
  16. Decimal formatting: (4.1.3)
  17. Constants and magic numbers: (4.1.4)
  18. Assignment operators: (4.1.5)
  19. Type casting: (4.1.6)
  20. Integer range and overflow: (4.1.7)
  21. Numeric precision: (4.1.7)
  22. Multiple alternatives: (4.2.2-3)
  23. Nested branches: (4.2.4)
  24. Boolean variables : (4.2.5)
  25. Boolean (logical) operators: (4.2.6)
  26. Loops and while statements: (4.3, 5.1-5.3)
  27. Strings and Characters (6.1)
  28. Arrays (6.2)

Exam Taking Tips

  • If you get stuck on a question, make your best guess and return later
  • If you are equally uncertain between two choices, go with your first impression
  • When writing code, do NOT add more than the problem asks for
  • You do not need to comment code for tests and exams
    • Unless specifically instructed to in the exam question
  • Use the full time available
    • Check your work if you finish early
  • Any questions?
Comments