Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.

Similar presentations


Presentation on theme: "C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2."— Presentation transcript:

1 C++ Functions

2 Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2

3 Objective 1: Implementing C++ functions What is a function? What is a function? Understand two difference function types and their structures. Understand two difference function types and their structures. How to implement each function? How to implement each function? 3

4 Structure of Essay 4 Version 1 Version 2 One point for each paragraph divide and conquer

5 Divide & Conquer in Software Development We implement app to finish a task. We implement app to finish a task. The task is divided into smaller tasks The task is divided into smaller tasks Implementing each simple task by a function Implementing each simple task by a function 5 divide processingIncomeTaxesgetIncomecomputeTaxesprintTaxes conquer

6 Paragraph vs Function Consists of one or more sentences, Consists of one or more sentences, Deals with one point Deals with one point Begins on a new usually indented line Begins on a new usually indented line Consists of one or more statements Consists of one or more statements Deals with one smaller task Deals with one smaller task Begins with a function name Begins with a function name 6

7 Function Types Standard Standard User-defined User-defined 7

8 8 Standard Functions Build-in functions Build-in functions Organized in 100+libraries Organized in 100+libraries How to use a build-in function? How to use a build-in function? Where are libraries Where are libraries Which function you want to use Which function you want to use Does the function need inputs Does the function need inputs

9 9 Standard Math Functions #include #include using namespace std; int main() { // Getting a double value double x; cout << "Please enter a real number: "; cin >> x; // Compute the ceiling and the floor of the real number cout << "The ceil(" << x << ") = " << ceil(x) << endl; cout << "The floor(" << x << ") = " << floor(x) << endl; } http://isocpp.org/wiki/faq/coding-standards#std-headers

10 Demo Using online IDE Using online IDE https://ideone.com/ https://ideone.com/ https://ideone.com/ ceil(2.5) =3 ceil(2.5) =3 floor(2.5)=2 floor(2.5)=2 10

11 Hands-on Experience (1) ceil(-2.5) =? ceil(-2.5) =? floor(-2.5)=? floor(-2.5)=? Hint Hint Copy and paste code to https://ideone.com/ using new input (-2.5) Copy and paste code to https://ideone.com/ using new input (-2.5)https://ideone.com/ 11

12 12 Standard Character Functions #include // input/output handling #include // character type functions using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; cout << "The toupper(" << ch << ") = " << (char) toupper(ch) << endl; cout << "The tolower(" << ch << ") = " << (char) tolower(ch) << endl; if (isdigit(ch)) cout << "'" << ch <<"' is a digit!\n"; else cout << "'" << ch <<"' is NOT a digit!\n"; } Explicit casting vs cctype

13 Demo http://ideone.com/lP7Txr http://ideone.com/lP7Txr http://ideone.com/lP7Txr At least two test cases are required At least two test cases are required Char is an alphabet (x) Char is an alphabet (x) Char is a digit (5) Char is a digit (5) 13

14 14 User-Defined Functions Implement a function Implement a function to determine if a triangle is to determine if a triangle is library does not include a standard function library does not include a standard function

15 15 Define a Function? Step #1 – Implement the function in.cpp Step #1 – Implement the function in.cpp Step #2 – publish the function in Step #2 – publish the function in before the main function of the program before the main function of the program for yourself for yourself a header file (.h file) a header file (.h file) for you or other people reuse for you or other people reuse Step #3 (if using header) – Use the functions in main.cpp Step #3 (if using header) – Use the functions in main.cpp

16 16 Syntactic Structure of a Function? The function header The function header ( ) ( ) The function body enclosed between The function body enclosed between { }

17 17 Example of User-defined Function double computeTax(double income) { if (income < 5000.0) return 0.0; if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); double taxes = 0.07 * (income-5000.0); return taxes; return taxes;}

18 18 double computeTax(double income) { if (income < 5000.0) return 0.0; if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); double taxes = 0.07 * (income-5000.0); return taxes; return taxes;} Example of User-defined Function Function header Descriptive name

19 19 Example of User-defined Function double computeTax(double income) { if (income < 5000.0) return 0.0; if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); double taxes = 0.07 * (income-5000.0); return taxes; return taxes;} Function header Function body

20 How to Improvement double computeTax(double income) { if (income < 5000.0) return 0.0; if (income < 5000.0) return 0.0; double taxes = 0.07 * (income-5000.0); double taxes = 0.07 * (income-5000.0); return taxes; return taxes;} 20 double computeTax(double income) { const double baseIncome=5000.0; const double baseIncome=5000.0; const double taxRates=0.07; const double taxRates=0.07; double taxes =-1.0; double taxes =-1.0; if (income < baseIncome) { if (income < baseIncome) { taxes=0.0; taxes=0.0; } else { } else { taxes = taxRates * (income-baseIncome); taxes = taxRates * (income-baseIncome); } return taxes; return taxes;} Original version Improved version refactoring

21 How to Improvement 21 double computeTax(double income, double baseIncome, double taxRates) { double baseIncome, double taxRates) { const double baseIncome=5000.0; const double baseIncome=5000.0; const double taxRates=0.07; const double taxRates=0.07; double taxes =-1.0; double taxes =-1.0; if (income < baseIncome) { if (income < baseIncome) { taxes=0.0; taxes=0.0; } else { } else { taxes = taxRates * (income-baseIncome); taxes = taxRates * (income-baseIncome); } return taxes; return taxes;} Improved version 2 refactoring double computeTax(double income) { const double baseIncome=5000.0; const double baseIncome=5000.0; const double taxRates=0.07; const double taxRates=0.07; double taxes =-1.0; double taxes =-1.0; if (income < baseIncome) { if (income < baseIncome) { taxes=0.0; taxes=0.0; } else { } else { taxes = taxRates * taxes = taxRates * (income-baseIncome); (income-baseIncome); } return taxes; return taxes;} Improved version

22 22 Function Header vs. Signature Example Example double computeTaxes(double) ; Unnamed Parameter Semicolon ;

23 23 TaxesMain.cpp #include #include using namespace std; // Function Signature double getIncome(string); double getIncome(string); double computeTaxes(double); double computeTaxes(double); void printTaxes(double); void printTaxes(double); int main() { // Get the income; double income = getIncome("Please enter the employee income: "); // Compute Taxes double taxes = computeTaxes(income); // Print employee taxes printTaxes(taxes);} double computeTaxes(double income) { if (income<5000) return 0.0; if (income<5000) return 0.0; return 0.07*(income-5000.0); return 0.07*(income-5000.0);} double getIncome(string prompt) { cout << prompt; cout << prompt; double income; double income; cin >> income; cin >> income; return income; return income;} void printTaxes(double taxes) { cout << "The taxes is $" << taxes << endl; cout << "The taxes is $" << taxes << endl;}

24 1. Copy and paste code 2. Add signatures 3. Replacing the red statements with refactored code 4. Test your code Solution: https://ideone.com/Y5 3Iwf Solution: https://ideone.com/Y5 3Iwf 24 #include #include using namespace std; // Function Signature // add signature here // add signature here int main() { // Get the income; double income = getIncome("Please enter the employee income: "); // Compute Taxes double taxes = computeTaxes(income); // Print employee taxes printTaxes(taxes);} double computeTaxes(double income) { if (income<5000) return 0.0; if (income<5000) return 0.0; return 0.07*(income-5000.0); return 0.07*(income-5000.0);} double getIncome(string prompt) { cout << prompt; cout << prompt; double income; double income; cin >> income; cin >> income; return income; return income;} void printTaxes(double taxes) { cout << "The taxes is $" << taxes << endl; cout << "The taxes is $" << taxes << endl;} Hands-on Experience (2)

25 25 Publish Your Functions Build libraries to be used by you and your customers Build libraries to be used by you and your customers Create header files to store function signatures Create header files to store function signatures

26 26 // This is a header file structure #ifndef TAXESRULES_H_ // check if a unique value _ #define TAXESRULES_H_ TaxesRules.h compiler directive #include other header files #endif //All functions signatures here _H_ //Never put functions implementation here

27 27 TAXESRULES_H_ #ifndef TAXESRULES_H_ TAXESRULES_H_ #define TAXESRULES_H_ TaxesRules.h #include using namespace std; /** purpose -- to get the employee income purpose -- to get the employee income input -- a string prompt to be displayed to the user input -- a string prompt to be displayed to the user output -- a double value representing the income output -- a double value representing the income*/ double getIncome(string); // purpose -- to compute the taxes for a given income // input -- a double value representing the income // output -- a double value representing the taxes double computeTaxes(double); void printTaxes(double); #endif

28 28 TaxesRules.cpp #include “TaxesRules.h" double computeTaxes(double income) { // need to refactor later // need to refactor later if (income<5000) return 0.0; if (income<5000) return 0.0; return 0.07*(income-5000.0); return 0.07*(income-5000.0);} double getIncome(string prompt) { cout << prompt; cout << prompt; double income; double income; cin >> income; cin >> income; return income; return income;} void printTaxes(double taxes) { cout << "The taxes is $" << taxes << endl; cout << "The taxes is $" << taxes << endl;}

29 Main Program File 29 TaxesMain.cpp #include “TaxesRules.h" int main() { // Get the income; // Get the income; double income = getIncome("Please enter the employee income: "); double income = getIncome("Please enter the employee income: "); // Compute Taxes // Compute Taxes double taxes = computeTaxes(income); double taxes = computeTaxes(income); // Print employee taxes // Print employee taxes printTaxes(taxes); printTaxes(taxes);}

30 Files Needed for User-defined Functions Head files Head files TaxesRules.h TaxesRules.h Implementation of the header files Implementation of the header files TaxesRules.cpp TaxesRules.cpp One driver/Main file One driver/Main file TaxesMain.cpp TaxesMain.cpp 30

31 31 Discussion: Why Do We Need Function Signature? For Information Hiding For Information Hiding Only publish function signatures in a header (.h) file Only publish function signatures in a header (.h) file Hide implementation details Hide implementation details For Function Abstraction For Function Abstraction We can change the implementation details from time to time to We can change the implementation details from time to time to Improve function performance Improve function performance make the customers focus on the purpose of the function, not its implementation make the customers focus on the purpose of the function, not its implementation

32 Objective 2: Sharing data among functions Global Variable (bad practice) Global Variable (bad practice) Passing parameters Passing parameters Value parameters Value parameters Reference parameters Reference parameters 32

33 Sharing Using Global Variables 33 http://ideone.com/L1ogIq Global var

34 Using Global Variables 34 x 0

35 Using Global Variables 35 x 0 int main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1

36 Using Global Variables 36 x 0 void main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1 void f2() { x += 4; x += 4; f1(); f1();} 2 4

37 Using Global Variables 37 45 x void main() { f2(); f2(); cout << x << endl ; cout << x << endl ;} 1 void f2() { x += 4; x += 4; f1(); f1();} 3 void f1() { x++; x++;} 4

38 Using Global Variables 38 45 x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 1 void f2() { x += 4; x += 4; f1(); f1();} 3 void f1() { x++; x++;} 5

39 Using Global Variables 39 45 x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 1 void f2() { x += 4; x += 4; f1(); f1();} 6

40 Using Global Variables 40 45 x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 7

41 Using Global Variables 41 45 x void main() { f2(); f2(); cout << x << endl; cout << x << endl;} 8

42 Using Global Variables 42

43 43 What is Bad About Using Global Vairables? Not safe! Not safe! If two or more programmers are working together in a program, one of them may change the value stored in the global variable without telling the others who may depend in their calculation on the old stored value! If two or more programmers are working together in a program, one of them may change the value stored in the global variable without telling the others who may depend in their calculation on the old stored value! Against The Principle of Information Hiding! Against The Principle of Information Hiding! this gives all functions the freedom to change the values stored in the global variables at any time this gives all functions the freedom to change the values stored in the global variables at any time

44 Example of Defining and Using Global and Local Variables 44

45 45 Example of Defining and Using Global and Local Variables x 0 Global variables are automatically initialized to 0

46 46 Example of Defining and Using Global and Local Variables x 0 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 1

47 Example of Defining and Using Global and Local Variables 47 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x ???? 3

48 Example of Defining and Using Global and Local Variables 48 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 3

49 Example of Defining and Using Global and Local Variables 49 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 4

50 Example of Defining and Using Global and Local Variables 50 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 2 void fun() { int x = 10; int x = 10; cout << x << endl; cout << x << endl;} x 10 5

51 Example of Defining and Using Global and Local Variables 51 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 6

52 Example of Defining and Using Global and Local Variables 52 x 4 int main() { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl;} 7

53 53 Passing Value Parameters Copy the values of the function call’s arguments to callee’s Copy the values of the function call’s arguments to callee’s Any changes in the callee’s value parameters don’t affect the original function arguments Any changes in the callee’s value parameters don’t affect the original function arguments

54 Example of Using Value Parameters 54 x 0 int main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 1

55 Example of Using Value Parameters and Global Variables 55 x 4 void main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 3 3

56 Example of Using Value Parameters 56 x 4 Int main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 3 4 8

57 Example of Using Value Parameters 57 x 4 int main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 2 void fun(int x ) { cout << x << endl; cout << x << endl; x=x+5; x=x+5;} 8 5

58 Example of Using Value Parameters 58 x 4 int main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 6

59 Example of Using Value Parameters 59 x 4 int main() { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl;} 7

60 60 Passing Reference Parameters Want to change the values of the original function arguments Want to change the values of the original function arguments double update (double & x); FFFF & ampersand

61 Example of Reference Parameters 61 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 1 x ? x 4

62 Example of Reference Parameters 62 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 3

63 Example of Reference Parameters 63 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 4 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 4 9

64 Example of Reference Parameters 64 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 2 x ? x 9 void fun( int & y ) { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5;} 5

65 Example of Reference Parameters 65 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} 6 x ? x 9

66 Example of Reference Parameters 66 int main() { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl;} x ? x 9 7

67 Hands-on Experience (3) 67 https://ideone.com/n0luWe

68 Summary Declare and Implementing C++ functions for reusing Declare and Implementing C++ functions for reusing Passing by value Passing by value Passing by reference Passing by reference 68

69 69 Function Overloading defined more than once defined more than once different data types or different number of parameters different data types or different number of parameters

70 Hands-on Experience (4) Add statement Add statement max(3.5, 2) max(3.5, 2) How to fix it? How to fix it? Modify the code at http://ideone.com/6iZRVM Modify the code at http://ideone.com/6iZRVM 70


Download ppt "C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2."

Similar presentations


Ads by Google