CNSP - Lecture #5 Chapter 6
BY PROF. RAFAEL ORTA
Disclosure: this presentation includes slides that were provided by the textbook publisher and Dr. Hnatyshin, some are adaptation, and some are an exact replica.
Last class we covered�
5.1 Introduction to Loops: The while Loop
5.2 Using the while loop for Input Validation
5.3 The Increment and Decrement Operators
5.4 Counters
5.5 Keeping a Running Total
5.6 Sentinels
Last class we covered�
5.7 The do-while loop
5.8 The for loop
5.9 Deciding Which Loop to Use
5.10 Nested Loops
5.11 Breaking Out of a Loop
5.12 Using Files for Data Storage
5.13 Creating Good Test Data
Topics 1 of 2
6.1 Modular Programming
6.2 Defining and Calling Functions
6.3 Function Prototypes
6.4 Sending Data into a Function
6.5 Passing Data by Value
6.6 The return Statement
6.7 Returning a Value from a Function
6.8 Returning a Boolean Value
Topics 2 of 2
6.9 Using Functions in a Menu-Driven Program
6.10 Local and Global Variables
6.11 Static Local Variables
6.12 Default Arguments
6.13 Using Reference Variables as Parameters
6.14 Overloading Functions
6.15 The exit() Function
6.16 Stubs and Drivers
6.1 Modular Programming
6.2 Defining and Calling Functions
Function Definition 1 of 2
name: the name of the function. Function names follow the same rules as variable names
parameter list: the variables that hold the values that are passed to the function when it is called
body: the statements that perform the function’s task
return type: data type of the value the function returns to the part of the program that called it
Function Definition 2 of 2
Function Header
int main()
Function Return Type
int main()
void printHeading()
{
cout << "\tMonthly Sales\n";
}
Calling a Function 1 of 2
printHeading();
Calling a Function 2 of 2
6.3 Function Prototypes 1 of 2
The compiler must know the following about a function before it is called
Function Prototypes 2 of 2
There are multiple ways to notify the compiler about a function before making a call to the function:
Prototype Notes
6.4 Sending Data into a Function
c = sqrt(a*a + b*b);
Parameters, Prototypes, and Function Headings
void evenOrOdd(int); or
void evenOrOdd(int num); // prototype
void evenOrOdd(int num) //header
Function Call Notes
Calling Functions with Multiple Arguments
When calling a function with multiple arguments
Calling Functions with �Multiple Arguments Illustration
displayData(height, weight); // call
void displayData(int h, int w)// header
{
cout << "Height = " << h << endl;
cout << "Weight = " << w << endl;
}
6.5 Passing Data by Value
Passing Data to Parameters by Value
evenOrOdd(val);
parameter in
evenOrOdd function
num
5
argument in
calling function
val
5
6.6 The return Statement
6.7 Returning a Value from a Function
Returning a Value – the return Statement
6.8 Returning a Boolean Value
Boolean return Example
bool isValid(int); // prototype
bool isValid(int val) // header
{
int min = 0, max = 100;
if (val >= min && val <= max)
return true;
else
return false;
}
if (isValid(score)) // call
…
Programming Style and return statements
A function may calculate a return value and use a single return statement. The previous example could be written as:
bool isValid(int val) // header
{
bool result;
int min = 0, max = 100;
if (val >= min && val <= max)
result = true;
else
result = false;
return result; // single return
}
…
6.9 Using Functions in a Menu-Driven Program
Functions can be used
Screen Management
Screen Management in a Menu-Driven Program
You can clear the screen to remove prior output while a program is running:
To allow the user enough time to read output before the screen clears, use code like:
cout << "Press the Enter key to continue.";
cin.get(); // clear the input buffer
cin.get(); // get the Enter key
6.10 Local and Global Variables
Local Variable Lifetime
Local and Global Variables
Initializing Local and Global Variables
Global Variables – Why ‘Use Sparingly’?
Global variables make:
Global Constants
Local and Global Variable Names
6.11 Static Local Variables
static int counter;
6.13 Using Reference Variables as Parameters
Reference Variables
void getDimensions(int&, int&);
Pass by Reference Example
void squareIt(int &); //prototype
void squareIt(int &num)
{
num *= num;
}
int localVar = 5;
squareIt(localVar); // localVar now
// contains 25
Reference Variable Notes
6.14 Overloading Functions
Overloaded Functions Example
If a program has these overloaded functions,
void getDimensions(int); // 1
void getDimensions(int, int); // 2
void getDimensions(int, float); // 3
void getDimensions(double, double);// 4
then the compiler will use them as follows:
int length, width;
double base, height;
getDimensions(length); // 1
getDimensions(length, width); // 2
getDimensions(length, height); // 3
getDimensions(height, base); // 4
6.16 Stubs and Drivers
Let’s see an example of a stub function