Modular Programming with Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Modular Programming with Functions

Description:

Structure charts are often used to represent the modular design. ... Part of Stick Figure. Step1 : Draw triangle. Step2: Draw rectangle. Step3: Draw hollow rectangle ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 13
Provided by: bina1
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Modular Programming with Functions


1
Modular Programming with Functions
  • B. Ramamurthy
  • Chapter 5

2
Introduction
  • Complex problems Divide and conquer
  • Reusability of code
  • Modular design of software to enhance readability
    and maintenance.
  • Abstraction
  • Information hiding

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
4
Libraries of Functions
  • We have used functions from many libraries
  • cmath pow, sin, cos
  • fstream open, close, get, set
  • iomanip setw, setprecision
  • iostream ltlt , gtgt
  • Programmers can define functions for use in their
    program programmer-defined functions.

5
Functions Definition
  • Function header
  • Function parameters
  • Function body
  • Function return value
  • In fact
  • int main()
  • return 0
  • is a function.

6
Example StickFigure


























7
Part of Stick Figure
















Step1 Draw triangle Step2 Draw
rectangle Step3 Draw hollow rectangle
8
drawTriangle(..)
for (int i3 0 i3 lt row i3) for (int k
1 klt offset k)
coutltlt ' ' for (int j 1 j
lt col j ) cout ltlt'' offset offset
-1 col col 2 coutltlt endl
9
drawRectangle(..)
for (int i4 1 i4 lt length i4) for
(int k 1 klt offset k)
coutltlt ' ' for
(int j 1 jlt width j) cout
ltlt'' coutltlt endl
10
drawHollowRectangle(..)
for (int i4 1 i4 lt length i4) for
(int k 1 klt offset k) coutltlt ' ' if
((i4 1 ) (i4 length)) for (int j 1
jlt width j) cout ltlt'' else c
out ltlt '' for (int k 1 klt width-2
k) cout ltlt ' cout ltlt
'' coutltlt endl
11
Function syntax
  • type functioName (parameters)
  • declare local variables/constants
  • statements

12
Example
  • int factorial (int n)
  • int prod
  • for (int i 1 i lt n i)
  • prod prod i
  • return prod
Write a Comment
User Comments (0)
About PowerShow.com