Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.

Similar presentations


Presentation on theme: "Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function."— Presentation transcript:

1

2 Lesson xx

3 Why use functions Program that needs a function Function header Function body Program rewritten using a function

4 1. Functions allow you to repeat a body of code without having to physically rewrite it many times. 2. Functions allow you to modularize code. This means that you can break down your problems into small blocks and each block can be put into a function. This makes a program easier to work with and debug. 3. Functions allow many people to work on one program at the same time. Each person writes a different function.

5 Write a program that: 1. Prints a row of 45 ‘*’s 2. Prints the contents of the variable a 3. Prints a row of 45 ‘*’s 4. Prints the contents of the variable b 5. Prints a row of 45 ‘*’s 6. Prints the contents of the variable c 7. Prints 2 rows of 45 ‘*’s

6

7 #include "stdafx.h" #include using std::cout; using std::endl; int main() { int a = 5, i; float b = 17.345; char c = 'q'; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " a = " << a; /************************************************/ cout << endl ; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b; /************************************************/ cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " c = " << c; /************************************************/ cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; system ("pause"); return 0; }

8 cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " a = " << a; (1) (2) (3) (4)

9 cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b;

10 cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl;

11 void funName ( ) ///function header { Body of code }

12 void funName ( ) Return type Function name Argument list

13 #include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = 17.345; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; } void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

14 void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

15 void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

16 void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

17 #include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = 17.345; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; }

18 #include "stdafx.h" #include using std::cout; using std::endl; void prtStar ( ); ///function prototype int main() { int a = 5,; float b = 17.345; char c = 'q'; prtStar ( ); cout << " a = " << a; /************************************************/ prtStar (); cout << " b = " << b; /************************************************/ prtStar (); cout << " c = " << c; /************************************************/ prtStar (); return 0; }

19 How to write a function function header { // body } void fun ( ) { //code goes here } How to call a function 1. function prototype / declaration 2. function call / invocation void fun ( ) ; ///function declaration void main ( ) {... fun ( ) ; ///function call... return 0; }

20 Why use functions Program that needs a function Function header Function body Program rewritten using a function


Download ppt "Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function."

Similar presentations


Ads by Google