Title: Review 2
1Review forExam 2
2Chapter 5 Outline
- writing value-returning functions
- program design with value-returning functions
- void functions and program design
- Function calling other functions
3Parameters vs. Arguments
parameters are variables that belong to the
function (in declaration and definition header),
used to receive data from the calling statement.
parameters
arguments appear in the function call, hold the
data to be passed to a function
arguments
parameters
4Parameter and Argument Matching
- A function call creates a memory cell for each
parameter and then copies the value of the
matching argument into that cell - Arguments and parameters are matched from left to
right based on their positions, i.e., the value
of the first argument is passed (assigned) to the
first parameter.
IMPORTANT!
The names of the arguments parameters do not
matter for this purpose Theyd better be of
compatible data types
5Value parameters passing by value
- What were doing here is passing by value
- Argument is evaluated
- Then a copy of that value is passed to the
corresponding parameter of the called function - If a function changes the value of one of its
parameters, that change will stay local to the
function - it will NOT affect the arguments value back at
the point of the call - There is another way, that lets functions change
their argument values if we choose to use it - Reference parameters
6Scope of variables
- The scope of a variable is the portion of the
program in which statements can use them - Scope for local variables and value parameters is
limited to the body of that function - Scope of (local) variables defined in main is
limited to the body of main - Global variables and constants are defined before
main and can be used in all functions - Read Section 9.4
include ltiostreamgt const double RATE
5.00 double function (int iHours)int main(
) double salary int hours salary
function(hours) double function(int
iHours) doulbe dSalary dSalary iHours
RATE return dSalary
7void (Non-Value Returning) Functions
declare a void function
call a void function
define a void function
8Chapter 6 Outline
- string variables
- cin and cout streams
- Reading strings with whitespace characters
- getline function
- cin.ignore function
9The string Library
- string variable is a sequence of characters,
taken together as a unit - Must include ltstringgt at top of program
- string library comparison, input/output, other
functions - Declaring string Variables
- string LastName LastName "Jones"
- String Literals
- Remember, use double-quote marks " for strings
that appear in the source code. - do not use " in response to run-time prompt
10Read a value into a string variable
- cin gtgt
- skips over any leading whitespace characters
(blanks, tabs, newlines) - copies non-whitespace characters it sees into the
string variable - stops when it hits the next whitespace character
- Its impossible to read a string with embedded
whitespace characters into a single string
variable using the gtgt operator
11Concatenating strings with
12Function getline
- getline (stream, string)
- stream is an input stream like cin
- string is a string variable
- Input (Reads) up to the next newline character
- read the users name into string variable name
- name would receive the value Jane Doe
13Mixing gtgt and getline
Linux
Windows
- Why didnt the user get a chance to enter her
name? - before executing getline(cin, name) the state
of cin is (Linux)the state of cin is
Jane Doe (Windows) - getline assigns a string with no characters to
name
When the next character in cin is the newline
character, then the call getline(cin, str_var)
assigns null string (i.e., no characters) to
str_var.
14Use the ignore function
- After execution of cin gtgt age
- the only character still in cin is
- After execution cin.ignore(80, \n)
- is discard and there will be no characters in
cin.
15Chapter 7 Outline
- Looping in programs
- while loops
- for loops
- do-while loops
16The while Loop
- Syntax
- while (Condition)
-
- Statements
-
- As long as the Condition is true, keep executing
Statements in the loop body - Statements can be either a single statement or a
group of statements within braces
17Simple while Example
Note in the while loop, initialization is
outside the loop.
n is loop control variable (LCV).
- The Loop Elements
- Initialization n 1
- Test Condition (n lt 3)
- Statement cout ltlt Test " ltlt n ltlt endl
- Update Condition n n 1
- What if the condition is never updated?
18The Infamous Infinite Loop
- A Loop That Never Terminates
- Possible reasons
- Loop specified wrong
- Example control variable can never attain exit
value - Never reaches exit condition
- Example forgot to update the control variable
- Bug in the loop body
- Example control variable reset somewhere in
loop body - Effect
- May be obvious
- Example output statement within loop
body (text starts streaming down your screen) - May be silent
- Just sits there, and sits there, and
19The for Loop
- Shorthand For Use With Fixed-Step Loops
- Syntax
- for (Initialize LCV Test LCV Update LCV)
-
- Statements
-
- Execute Initialize LCV
- While Test LCV is true
- Execute Statements
- Execute Update LCV
- Example
- int n
- for (n 0 n lt 10 n)
-
- cout ltlt n is " ltlt n ltlt endl
NOTE semicolons,not commas!!
// For n from 0 to 10 by 1
20The for vs. the while Constructs
- for (n 0 n lt 10 n)
-
- cout ltlt n is " ltlt n ltlt endl
-
- is the same as
Initialization Test Statements Update
n 0 while (n lt 10) cout ltlt n is ltlt n
ltlt endl n
Value of n after the while and for loop?
21Which variables to initialize
- Using for loops to process information for a
certain number of people, items, or groups of
data
22- Using variables for the initial and test values
of the LCV
23The do - while Loop
- do
- Statements
- while (Condition)
- Execute Statements if Condition is true,
then execute Statements again, ... - Statements always get executed at least once
(needed)
Statements
false
Test
true
(continue withthe statement following the do
while loop)
24do-while vs. while
- Test at the bottom of the loop
- Loop body must be executed at least once
- Test not done until after execution of body
- Test at the top of the loop
- Loop body executed zero or more times
- If test fails first time through, loop body never
executes
Coding Style Put the while clause on the same
line as the
25Chapter 8 Outline
- Guidelines for constructing a loop structure
- Loops that exit on completion of the task
- Dont know ahead of time how many times to loop
- Have to recognize exit criterion when it comes up
- Exit a loop using the break statement
- Nested loop
26What to check?
- Initialization
- Check the initial value of loop control or other
key variables - Order
- Check where the loop control variables be updated
- Relational Operator
- Check the relational operator in the loop exit
(condition) test - lt or lt
- or !
- First and Last Execution of Loop Body
27Multiple Reasons for a Loop Exit
- When there are two or more possible reasons for
exiting a loop, a compound condition is used in
the loop exit test. - Use an if else statement after the loop to
determine which condition caused the exit
do cout ltlt "The residency status should be
R or N. \n" cout ltlt "Please enter the
residency status (R or N) " cin gtgt
chResStatus while (chResStatus ! 'R'
chResStatus ! 'N') if (chResStatus R)
cout ltlt The student is resident. ltlt
endl else cout ltlt The student is
nonresident. ltlt endl
28The Middle-Exit Loop
- Uses the break Statement to exits innermost loop
construct - int Number, Count
- int Sum 0
- while (true)
-
- cout ltlt "Enter a number "
- cin gtgt Number
- if (Number lt 0)
- break
- Sum Number
- count
-
- cout ltlt Sum is ltlt Sum ltlt endl
- count ltlt Count is ltlt Count ltlt endl
29Nested Loops - Loop Within a Loop
- int Number, Count
- int Sum 0
- while (true)
-
- cout ltlt "Enter a number "
- cin gtgt Number
- if (Number lt 0)
- break
- for (int i0 iltNumber i)
-
- Sum i
- if (Sum gt 10)
- break
-
- cout ltlt Sum is ltlt Sum ltlt endl
30Nested Loops - Loop Within a Loop
31Chapter 9 Outline
32Reference parameters passing by reference
- Value parameters
- a copy of that value is passed to the
corresponding parameter of the called function - If a function changes the value of one of its
parameters, that change will stay local to the
function - it will NOT affect the arguments value back at
the point of the call
- Reference parameters
- A reference parameter allows a function call to
give a value to or update the current value of a
variable argument appearing in the call. - The ampersand, , between the parameters data
type and its name, says the parameter is a
reference parameter
33Reference parameters Passing by Reference
void f(int a, int b) int main() int
x 3, y 5 f(x, y) cout ltlt x ltlt
ltlt y ltlt endl return 0 void f(int a,
int b) a b 1 b 10
34Swapping two values
35Designing Writing Programs
- Assignment 3 (function)
- Assignment 4 (loops)
- Assignment 5 (nested loops)
- Assignment 2 (if else)
36What else to review?
- All the Required Exercises posted on the web site