Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor - C. BoyleFall Semester - 2015

Similar presentations


Presentation on theme: "Instructor - C. BoyleFall Semester - 2015"— Presentation transcript:

1 Instructor - C. BoyleFall Semester - 2015 cboyle@cs.odu.edu

2 Terms invoke a function (call a function) construct a function (declare or write a function) value returning function void function parameter formal parameter actual parameter function header

3 Terms function body return statement return type function prototype flow of execution value parameters reference parameters local variables

4 4 Coding Cable Company Program –Ver 1IF ELSE –Ver 2SWITCH –Ver 3Functions

5 5 Coding Some functions return a value Exercises 4, 9, 10, chapter 6 –Ch6_Exer4.cpp –Ch6_Exer9_and_10 Value returning functions are used in an expression In these examples the parameters are passed by value

6 6 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // method printer will print out the symbol number times on a single line. void printer (int number, char symbol) { int counter; //local counter for loop control counter = 1; while (counter <= number ) { counter++; cout << symbol; } }// end of printer { {

7 7 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void printer (int, char); void treePrinter () ; void diamondPrinter () ; void rectanglePrinter ( int across, int down); const char BLANK =' ' ; const char PLUS ='+' ; const char STAR ='*' ; const char POUND ='#' ;

8 8 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // call printer several times printer ( 6,'%'); cout << endl; printer ( 12, 'A'); cout << endl; treePrinter () ; cout << endl << endl; system ( "pause");

9 9 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // call diamondPrinter, and rectanglePrinter functions diamondPrinter () ; cout << endl << endl; system (“pause”); rectanglePrinter ( 10, 4); rectanglePrinter ( 4,10 ) ; cout << endl << endl; system ("pause"); return 0; // end of main

10 10 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void printer(int number, char symbol) // method printer will print out the symbol number times on a single line. { int counter; //local counter for loop control counter = 1; while (counter <= number ) { counter++; cout << symbol; } } // end of printer -------------------------------------------------

11 11 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp void treePrinter () { cout << endl << endl; printer (9,BLANK); printer (1,STAR); cout << endl; printer (8,BLANK); printer (3,STAR); cout << endl; printer (7,BLANK); printer (5,STAR); ……………………………. ………. printer (8,BLANK); printer (3,POUND); cout << endl; printer (8,BLANK); printer (3,POUND); cout << endl; } // end of printer -------------------------------------------------

12 12 Coding Some functions are void functions In function printer, the parameters are passed by value –PrinterCalls.cpp // print a diamond shape { int bCount ; int lCount ; ……………….. See the code….. // print a diamond shape void rectanglePrinter ( int across, int down) { for ( int d = 1 ; d <= down ; d++ ) { printer (across, '#'); cout << endl; } } // end of rectanglePrinter

13 13 Coding Some functions are void functions Some functions pass the parameters passed by value Some functions pass the parameters passed by reference –Value_or_Reference.cpp //A swap that doesn't really swap! void up(int a) { a = a + 5; cout << "up a = " << a << endl; } //A swap that really swaps! void down(int& x) { x = x - 5; cout << "down x = " << x<< endl; }

14 void swap1 (int a, int b) { int temp; temp = a; a = b; b = temp; }

15 15 Coding Some functions are void functions Some functions pass the parameters - by value Some functions pass the parameters - by reference –Swap_it.cpp //A swap that doesn't really swap! void swap1 (int a, int b) { int temp; temp = a; a = b; b = temp; } //A swap that really swaps! void swap2 (int& a, int& b) { int temp; temp = a; a = b; b = temp; }

16 Chapter 6 Write a program to input and calculate the cost of ODU football tickets. Use functions. How many? Student prices Adult prices Senior prices

17 Summary 1 Project 2 Quizzes….. Read Chapter 6 - Functions! Again!

18 QUESTIONS?


Download ppt "Instructor - C. BoyleFall Semester - 2015"

Similar presentations


Ads by Google