1 / 19

Function Introduction

Function Introduction . Lesson xx. Objectives. Why use functions Program that needs a function Function header Function body Program rewritten using a function. Why Use Functions. Functions allow you to repeat a body of code without having to physically rewrite it many times .

pahana
Download Presentation

Function Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Function Introduction Lesson xx

  2. Objectives Why use functions Program that needs a function Function header Function body Program rewritten using a function

  3. Why Use Functions • Functions allow you to repeat a body of code without having to physically rewrite it many times. • 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. • Functions allow many people to work on one program at the same time. Each person writes a different function.

  4. Program Definition Write a program that: • Prints a row of 45 ‘*’s • Prints the contents of the variable a • Prints a row of 45 ‘*’s • Prints the contents of the variable b • Prints a row of 45 ‘*’s • Prints the contents of the variable c • Prints 2 rows of 45 ‘*’s

  5. Program Output

  6. Program Listing #include "stdafx.h" #include <iostream> 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; }

  7. cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " a = " << a; Code to Print 45 ‘*’s & Variable a (1) (2) (3) (4)

  8. cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b; Code to Print 45 ‘*’s & Variable b

  9. cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; Code to Print 45 ‘*’s

  10. void funName ( ) ///function header { Body of code } Function Setup

  11. Function Header void funName( ) Function name Return type Argument list

  12. Program Rewritten Using Functions #include "stdafx.h" #include <iostream> 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 (); prtStar (); return 0; } void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }

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

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

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

  16. Function Prototype / Declaration #include "stdafx.h" #include <iostream> 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 (); prtStar (); return 0; }

  17. Function Call / Invocation #include "stdafx.h" #include <iostream> 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 (); prtStar (); return 0; }

  18. Function Review 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; }

  19. Summary Why use functions Program that needs a function Function header Function body Program rewritten using a function

More Related