Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modular Programming with Functions

Similar presentations


Presentation on theme: "Modular Programming with Functions"— Presentation transcript:

1 Modular Programming with Functions
B. Ramamurthy Chapter 5 2/21/2019 BR

2 Introduction Complex problems: Divide and conquer Reusability of code
Modular design of software to enhance readability and maintenance. Abstraction Information hiding 2/21/2019 BR

3 Structure Charts Structure charts are often used to represent the modular design. Here is an example of a structure chart for StickFigure. main drawTriangle drawHollowRectangle drawRectangle 2/21/2019 BR

4 Libraries of Functions
We have used functions from many libraries: cmath : pow, sin, cos fstream : open, close, get, set iomanip: setw, setprecision iostream : << , >> Programmers can define functions for use in their program: programmer-defined functions. 2/21/2019 BR

5 Functions Definition Function header Function parameters Function body
Function return value In fact int main() { return 0; } is a function. 2/21/2019 BR

6 Example: StickFigure * 2/21/2019 BR *** ***** ******* ********* * *
* * ************************************************************************ 2/21/2019 BR

7 Part of Stick Figure * *** ***** ******* ********* * *
* * Step1 : Draw triangle Step2: Draw rectangle Step3: Draw hollow rectangle 2/21/2019 BR

8 drawTriangle(..) for (int i3 = 0; i3 < row; i3++)
{ for (int k = 1; k<= offset; k++) cout<< ' '; for (int j = 1; j <= col; j++ ) cout <<'*'; offset = offset -1; col = col +2; cout<< endl; } 2/21/2019 BR

9 drawRectangle(..) for (int i4 = 1; i4 <= length; i4++)
{ for (int k = 1; k<= offset; k++) cout<< ' '; for (int j = 1; j<= width; j++) cout <<'*'; cout<< endl; } 2/21/2019 BR

10 drawHollowRectangle(..)
for (int i4 = 1; i4 <= length; i4++) { for (int k = 1; k<= offset; k++) cout<< ' '; if ((i4 ==1 ) || (i4 == length)) { for (int j = 1; j<= width; j++) cout <<'*'; } else { cout << '*'; for (int k =1; k<= width-2; k++) cout << ‘ '; cout<< endl; 2/21/2019 BR

11 Function syntax type functioName (parameters) {
declare local variables/constants statements } 2/21/2019 BR

12 Example int factorial (int n) { int prod;
for (int i = 1; i <= n; i++) prod = prod *i; return prod; } 2/21/2019 BR


Download ppt "Modular Programming with Functions"

Similar presentations


Ads by Google