Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num; float power, squareRoot, absolute; cout << "Please input.

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Computer Science 1620 Loops.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 7 Functions.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 Scope Lifetime Functions (the Sequel) Chapter 8.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS],
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 8 Functions.
Chapter 7: User-Defined Functions II
User-Defined Functions
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
Chapter 9 Scope, Lifetime, and More on Functions
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Functions Imran Rashid CTO at ManiWeber Technologies.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Presentation transcript:

Functions 1

Example: Power, Square Root and Absolute values of a number #include … float num; float power, squareRoot, absolute; cout << "Please input a float number: "; cin >> num; power = pow ( num, 2 ); cout << "The power is " << power << endl; if ( num > 0 ) { squareRoot = sqrt ( num ); cout << "The square root is " << squareRoot << endl; } else cout << num << " is negative. No squre root!" << endl; absolute = fabs ( num ); cout << "The absolute value is " << absolute << endl; 2

Math Functions in Need to use #include or #include float pow ( float base, float exponent ); float sqrt ( float num ); float fabs ( float num ); They are functions! These are function prototypes! 3

What other functions have you learned? bool cin.eof(); int string.length(); string string.substr(); smanip setprecision(int n); smanip setw(int n); How to define and use your own function? 4

Example: MAX of 2 numbers #include using namespace std; int Max ( int num1, int num2 ); int main() { int num1, num2, maxResult; cin >> num1 >> num2; maxResult = Max ( num1, num2 ); cout << "The max is " << maxResult << endl; return 0; } int Max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } // function prototype (function declaration) // function heading function definition // function call 5

Review: Element of a function int Max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } Return Data TypeFunction NameParameter List Function Body 6

Function Prototype C++ programmer usually place all supporting functions after the main function. C++ rule: identifiers must be declared before use! Place a function prototype before the main function as a declaration. Two ways to declare a function: int max ( int num1, int num2 ); OR int max ( int, int ); 7 Add a semicolon to the function heading, and you get the function prototype!

Function Prototype #include using namespace std; int max ( int num1, int num2 ); int main() { … maxResult = max ( num1, num2 ) … } int max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } #include using namespace std; int max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } int main() { … maxResult = max ( num1, num2 ) … } = 8 Common Practice !

Review: Two Types of Functions Void Function Return data type is void Don’t need a return statement in the function Function call format: functionName(parameters); Value-Return Function Return data type is some real data type ( int, float, string ) Need a return statement to return a value to the caller function Function call format: var = functionName(parameters); OR use functionName(parameters) as a variable. 9

Formal parameters vs. Actual parameters (Arguments) Formal parameters appears in a function heading. int max ( int num1, int num2 ); Used in the callee function. Actually parameters (Arguments) appear in a function call. result = max ( 2, 3 ); During a function call, an argument can be a literal, variable, or expression. result = pow( 3, 2 ); result = fabs ( num ); result = sqrt ( num / ); A function is not required to have arguments. eof(); Used in the caller function. 10

Void Function #include using namespace std; const int HEIGHT = 10; void PrintLine(int height); int main() { int height = HEIGHT; while ( height > 0 ) { PrintLine(height); height --; } return 0; } void PrintLine(int height) { string spaceLine = ""; string dollarLine = ""; int count = 0; while ( count < height - 1 ) { spaceLine += " "; count ++; } count = 0; while ( count < 2 * ( HEIGHT - height ) + 1 ) { dollarLine += "$"; count ++; } cout << spaceLine << dollarLine << endl; } 11 Formal parameter: height Actual parameter: height  They can have the same name!

Value-Return Function #include using namespace std; float grossPay(float payRate, float hour); int main() { float hours; float rate; float salary; cout << "Please enter your pay rate “ << “(per hour): "; cin >> rate; cout << "Please enter your working “ << “hours: "; cin >> hours; salary = GrossPay(rate, hours); cout << "Your salary is " << salary << “.” << endl; return 0; } float GrossPay(float payRate, float hour) { float salary; if (hour <= 40) salary = hour * payRate; else { salary = ( hour - 40 ) * payRate * * payRate; cout << endl << "You have " << ( hour - 40 ) << " hours overtime." << endl; } return salary; } 12 Formal parameter: payRate, hour Actual parameter: rate, hour  They can have different names!

13 Function Names Meaningful names Names for functions and methods with a return type of void should generally be verb phrases such as ComputePay() and PrintOrders(). Names for functions and methods with other return types should generally be nouns or noun phrases such as Average() and MonthlySalary(). Capitalize the first letter of each word! Formal and actual parameters can have the same name They are different variables in different scopes Normally they have different names

Activation Record There is one activation record for each function call. An activation record contains: Local variables of the function called Return address Parameters for the function called Locals Return Address Parameters

Activation Record for GrossPay int main() { float hours; float rate, salary; cout << "Please enter your pay rate “ << “(per hour): "; cin >> rate; cout << "Please enter your working “ << “hours: "; cin >> hours; salary = GrossPay(rate, hours); cout << "Your salary is " << salary << “.” << endl; return 0; } float GrossPay(float payRate, float hour) { float salary; if (hour <= 40) salary = hour * payRate; else { salary = ( hour - 40 ) * payRate * * payRate; cout << endl << "You have " << ( hour - 40 ) << " hours overtime." << endl; } return salary; } 15 hours rate salary payRate hour salary 10 Input values: ?475 ? ? ?

16 Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope (Later in this semester.)

17 Statement Block Between a pair of matching braces e.g. The body of a function int main() { int alpha = 10; // A block for if statement if (alpha > 3) { int num; cin >> num; alpha += num; } return 0; }

18 Local Scope The scope of an identifier declared inside a block extends from the point of declaration to the end of that block. int main() { int alpha = 10; // A code block if (alpha > 3) { int num; cin >> num; alpha += num; } cout << "num = “ << num; // Run time error! return 0; }

Nested Local Scope local identifiers have name precedence! The scope of an identifier does not include any nested block that contains locally declared identifier of the same name! 19 int main () { int num = 0; string name = “Kyle”; while ( num < 5 ) { string name = “Alice”; cout << name << num << endl; num ++; } cout << endl << name; return 0; } What is the output? Alice0 Alice1 Alice2 Alice3 Alice4 Kyle

20 Global Scope The scope of an identifier declared outside all functions (and classes) extends from the point of declaration to the end of the entire source file. Programming Rules: No global variables!

Scope of a Function Name A function name has global scope. Function definitions cannot be nested within function definitions! 21 int main () { int num = 0; string name = “Kyle”; while ( num < 5 ) { void PrintAlice ( int count ) { string name = “Alice”; cout << name << count << endl; } num ++; } cout << endl << name; return 0; } // Can we do that? // NO!

22 Scope of Function Parameters Formal parameters Local scope Same as local variable Cannot reference it outside the function Receive values on function call Actual parameters (no global variables) Local scope Cannot reference it inside the called function

Example: GrossPay #include using namespace std; float GrossPay(float payRate, float hour); int main() { int hour; float rate; float salary; cout << "Please enter your pay rate “ << “(per hour): "; cin >> rate; cout << "Please enter your working “ << “hours: "; cin >> hour; salary = GrossPay(rate, hour); cout << "Your salary is " << salary << “.” << endl; return 0; } float GrossPay(float payRate, float hour) { float salary; if (hour <= 40) salary = hour * payRate; else { salary = ( hour - 40 ) * payRate * * payRate; cout << endl << "You have " << ( hour - 40 ) << " hours overtime." << endl; } return salary; } 23 actual parameters formal parameters scope

24 Example: Using a flag float DoIt(int num, char op); int main() { int base; float result; char choice; cout << “Enter a number: ”; cin >> base; cout << “C for Cube and S for Square Root: ”; cin >> choice; while (choice != ‘C’ && choice != ‘S’) { cout << “C for Cube and S for Square Root: ”; cin >> choice; } result = DoIt(base, choice); cout << “The result: ” << result; return 0; } // // Precondition: op is ‘C’ or ‘S’ // Postcondition: the cube of // num is computed when op is // ‘C’, and square root of num // is computed when op is ‘S’. // parameters: in, in // float DoIt(int num, char op) { if (op == ‘C’) result = pow(num, 3); else result = sqrt(num); return result; } // What is wrong? // Result not declared in the // function!

25 Example: Using a flag int DoIt(int num, char op); int main() { int base; float result; char choice; cout << “Enter a number: ”; cin >> base; cout << “C for Cube and S for Square: ”; cin >> choice; while (choice != ‘C’ && choice != ‘S’) { cout << “C for Cube and S for Square: ”; cin >> choice; } result = DoIt(base, choice); cout << “The result: ” << result; return 0; } // // Precondition: op is ‘C’ or ‘S’ // Postcondition: the cube of // num is computed when op is // ‘C’, and square root of num // is computed when op is ‘S’. // parameters: in, in // float DoIt(int num, char op) { float result; if (op == ‘C’) result = pow(num, 3); else result = sqrt(num); return result; } // The two variables // result have the same // name, but different!

Programming Ground Rules All functions, including the main function, should be no more than 30 lines long, from the left brace to the right brace, inclusive. Every function and method, except the main function, must have a comment which describes what it does and whether the parameters are input, output, or input and output. Be sure to document the purpose of any parameter which isn't obvious from its name alone. Thus function and method headers must have the following format: // // A few lines describing what the function or method does. // params: (in/out/inout,...) // result_type function_name(type1 param1, type2 param2,...) 26

Function Parameters IN parameters: The value of the actual parameter is passed into the function and assigned to the formal parameter. OUT parameters: The value of formal parameter is passed out of the function and assigned to the actual parameter. InOut Both In and Out. 27

Example: IN Parameter 28 #include using namespace std; float GrossPay(float payRate, float hour); int main() { int hour; float rate; float salary; cout << "Please enter your pay rate “ << “(per hour): "; cin >> rate; cout << "Please enter your working “ << “hours: "; cin >> hour; salary = GrossPay(rate, hour); cout << "Your salary is " << salary << “.” << endl; return 0; } The value of rate and hour in the main function is passed to payRate and hour in the grossPay function. Can we write a function to input rate and hour ? How to pass the values back to the main function?

Example: OUT Parameter 29 float GrossPay(float payRate, float hour); void GetInput(float& payRate, float& hoursWorked); int main() { float hour, rate, gross; // Call function getInput() to get two values GetInput(rate, hour); // Call function grossPay to get one value gross = GrossPay(rate, hour); // display result cout << "Your salary is " << gross << endl; return 0; }

Function definition of GetInput 30 // // The function inputs payRate and hoursWorked and // pass both values back to the calling function. // Parameters: (out, out) // void GetInput(float& payRate, float& hoursWorked) { cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked; return; // in void functions, using return is optional. } // The function does input with prompt, but OUT parameters! // How can it pass two values back? // Out Parameters: & // Statement return can pass only one value back!

Function definition of GrossPay 31 // // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular pay rate. // Parameters: (in, in) // float GrossPay(float payRate, float hour) { if (hour > REG_HOURS) payRate = (hour - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hour * payRate; return payRate; } // No local variable // payRate is used to store the result

Reference and Value Parameters float GrossPay(float payRate, float hour); void GetInput(float& payRate, float& hoursWorked); Value parameter: No & DataType identifier, … The value of actual parameter is passed to the formal parameter Reference Parameter: With & DataType& identifier, … The address of actual parameter is passed to the formal parameter Does the actual parameter change its value when the corresponding formal parameter changes its value? Value parameter (no &): NO Reference parameter (&): YES 32

Reference and Value Parameters Function call using value parameters: gross = GrossPay(rate, hour); Function call using reference parameters: GetInput(rate, hour); 33 Memory rate hour payRate hour mainGrossPay Memory rate hour &payRate &hoursWorked mainGetInput float GrossPay(float payRate, float hour); void GetInput(float& payRate, float& hoursWorked);

Trace Functions with OUT parameters 34 Input: GetInput(rate, hour); // Reference parameters gross = GrossPay(rate, hour);// Value parameters main() GetInput() GrossPay() hour rate gross payRate hoursWorked &rate &hour 450 ? ? ? payRate hour salary ?450

Example: InOut Parameter int main() { float score, highest, lowest; int scoreCount = 0; cin >> score; while (!cin.eof()) { if (scoreCount == 0) { highest = score; lowest = score; } else { if (score > highest) highest = score; if (score < lowest) lowest = score; } scoreCount ++; cin >> score; } cout << “Highest: “ << highest << “\nLowest: “ << lowest; return 0; } // Task: Use a function to update highest and lowest. 35

Example: InOut Parameter 36 // // The function uses the value of // num to update the // values of max and min. // Parameters: ( In, InOut, InOut ) // void UpdateMaxMin( float num, float& max, float& min ) { if (num > max) max = num; if (num < min) min = num; } void UpdateMaxMin ( float, float&, float& ); int main() { float score, highest, lowest; int scoreCount = 0; cin >> score; while (!cin.eof()) { if (scoreCount == 0) { highest = score; lowest = score; } else UpdateMaxMin ( score, highest, lowest ); scoreCount ++; cin >> score; } cout << "Highest: " << highest << "\nLowest: " << lowest; return 0; }

Example: InOut Parameter Function call: UpdateMaxMin ( score, highest, lowest ); Function Definition: void UpdateMaxMin( float num, float& max, float& min ) 37 Memory highest lowestmin max mainupdateMaxMin 10 score 2 num20 &highest &lowest

Trace Functions with InOut parameters 38 Input: updateMaxMin ( score, highest, lowest ); //Function call void updateMaxMin( float num, float& max, float& min ) //Function definition

Value Parameter and Reference Parameter What can the actual parameter be for a value parameter? A VALUE! Literal value(magic number) Variable (initialized!) Expression (with all variables initialized) What can the actual parameter be for a reference parameter? AN ADDRESS! Literal value:NO Expression:NO Variable:YES initialized or uninitialized 39

Actual Parameter Does the actual parameter change its value when the corresponding formal parameter changes its value? Value Parameter NO! Reference Parameter YES! 40

Function Calls Main function can call another function A A function can also call another function B 41 mainAB CallerCallee CallerCallee

Decompose the program by functions Modularity Any function or method (including the main function) can have at most 30 lines. A function should implement ONE basic functionality. Functions and methods which focus on computation should not produce output using cout statements. Check: Can I replace the loop body with a function? Can I use a function to get valid input? Can I use a function to do some particular computation? Is there any activity that is repeated multiple times in the program? 42

Design Function Interface What parameters should be passed? How? In, Out, or InOut? In: pass by value. Recommended! Out: pass by reference. InOut: pass by reference. If possible, try to avoid passing reference parameters! Because it might cause unexpected values changes of the caller’s variables. 43

Example: Bad Functions float GetRate(float rate) { cout << “Enter the rate: ”; cin >> rate; return rate; } // Bad: Why use parameter? float GetRate() { float rate; cout << “Enter the rate: ”; cin >> rate; return rate; } // Good: Use local variable! 44 float GrossPay(float rate, float hour) { float pay = rate * hour; cout << “The pay is “ << pay; return pay; } // Bad: No output in calculation functions! void Power ( int& base, int& exponent, int& result ) { result = 1; while ( exponent > 0 ) { result = result * base; exponent --; } } // Bad: No need to use reference parameter!

Example: Print a chess board Pseudo Code: while height is not 8 yet if line# is odd, then print an odd row otherwise, print an even row Print an odd row: while cell height is not enough while cell width is not enough if cell# is odd print N spaces otherwise, print N Print an even row: while cell height is not enough while cell width is not enough if cell# is odd print N otherwise, print N spaces 45 Do we need two print row functions? Use a flag to indicate odd or even row!

Example: Print a chess board Pseudo Code: while height is not 8 yet if row# is odd, then print an odd row otherwise, print an even row Print a row: while cell height is not enough while cell width is not enough if cell# is odd print N spaces if row# is even, or print N if row# is odd otherwise, print N if row# is even, or print N spaces if row# is odd 46 The function printRow is too long! replace the circled code with function printLine! Further pass the flag to printLine.

Example: Print a chess board 47 int main() { int count = 0; while ( count < BOARD_WIDTH ) { printRow ( count ); count ++; } return 0; } void printRow ( int rowNum ) { int countHeight = 0; while ( countHeight < CELL_HEIGHT ) { printLine ( rowNum ); countHeight ++; } void printLine ( int rowNum) { int countWidth = 0; int countCell = 0; while ( countWidth < BOARD_WIDTH ) { countCell = 0; if ( countWidth % 2 == 0 ) while ( countCell < CELL_WIDTH ) { if ( rowNum % 2 == 0) cout << EMPTY; else cout << FILL; countCell ++; } else while ( countCell < CELL_WIDTH ) { if ( rowNum % 2 == 0) cout << FILL; else cout << EMPTY; countCell ++; } countWidth ++; } cout << endl; } const int BOARD_WIDTH = 8; const int CELL_WIDTH = 10; const int CELL_HEIGHT = 4; const char FILL = const char EMPTY = ' '; Can it be further decomposed?

Summary Function definition Function prototype Void functions vs. value-return functions Formal parameters vs. actual parameters Scope of variables Scope of function parameters Scope of function names Parameter passing: In, Out and InOut Value parameters vs. reference parameters Program decomposition 48

More about getting input cin.get() cin.ignore() getline(cin, string) 49

cin.get(char&) the >> operator skips any leading whitespace or new line in the input stream. What if we want to read the whitespace? 50 char ch1, ch2, ch3; cin >> ch1 >> ch2 >> ch3; //OR cin.get(ch1); cin.get(ch2); cin.get(ch3); //Input: a b c //ch1 = ‘a’, ch2 = ‘b’, ch3=‘c’ //ch1 = ‘a’, ch2 = ‘ ’, ch3=‘c’

cin.ignore() cin.ignore(200, ‘\n’): skip the next 200 input characters OR skip characters until a newline character is read whichever comes first! 51 string str; cin.ignore(20, '\n'); cin >> str; cout << endl << str; If the input is “This is a test to see which string will be input.”, what do you think is the input?

getline(cin, string) How to read a complete line of input? getline(cin, string) will read everything until it reaches a newline character. 52