200 likes | 333 Vues
This lesson explores the use of functions in programming, focusing on their advantages and practical applications. Functions allow programmers to avoid code duplication, modularize their code, and facilitate team collaboration through separate function development. A sample program demonstrates how to implement a simple function to print a specified number of asterisks and the contents of various variables. The rewritten program illustrates function creation with clear definitions, structures, and calls for better readability and maintenance.
E N D
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. • 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.
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
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; }
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)
cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; cout << " b = " << b; Code to Print 45 ‘*’s & Variable b
cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; Code to Print 45 ‘*’s
void funName ( ) ///function header { Body of code } Function Setup
Function Header void funName( ) Function name Return type Argument list
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; }
Function prtStar ( ) void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }
Variable Declaration in prtStar ( ) void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }
Function prtStar ( ) void prtStar ( ) { int i; cout << endl; for (i = 0; i < 45; i++) { cout << "*"; } cout << endl; }
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; }
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; }
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; }
Summary Why use functions Program that needs a function Function header Function body Program rewritten using a function