You are on page 1of 17

7 Program Structure and

O
Organisation:
Organisation
i i : Functions
F
i
COL100 - Introduction to Computer
p
Science
II Semester 2015-2016
Department of Computer Science and Engineering
Indian Institute of Technology Delhi

Top-down Design:
TopSub--routines
Sub
Larger task is divided into
smaller tasks or sub
subroutines

Program

each sub-routine solves a subproblem


bl

Program is organised as a
sub-routines
hierarchy of sub
routines
Easier to manage

SubRoutine1

modular design/step-wise
refinement
fi
t
isolation of faults
p
concurrent development
Feb 2016

(C) P. R. Panda, IIT Delhi

SubRoutine2

SubRoutine4

SubRoutine3

SubRoutine5

Sub--Programs
Sub
Top-down design and step-wise
refinement
fi
t
Modularity
y of p
programs
g
programs divided into modules
each module performs a sub-task
sub task
enable software reuse

Programming construct: sub


sub-program
program
also sub-routine/procedure/function
Feb 2016

(C) P. R. Panda, IIT Delhi

Sub--Programs: Motivation
Sub
Algorithm DotProduct can
b applied
be
li d on vectors
t
It can also be used in
Matrix Multiplication

n-1

Prod = Xk Yk
k=0

n-1

Ci,j = Ai,k Bk,j


k=0

Feb 2016

(C) P. R. Panda, IIT Delhi

Sub--Programs:
Sub
g
Motivation
We had developed a
specialised version of
DotProduct

c[i][j]
j = 0;
for (k = 0; k < 4; k++) {
c[i][j] += A[i][k] * B[k][j];
}

main()
{
float a[4][4], b[4][4], c[4][4];
.../* read values into arrays */
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
c[i][j] = 0;
for (k = 0; k < 4; k++) {
c[i][j] += A[i][k] * B[k][j];
}
}
}

DotProduct
Feb 2016

(C) P. R. Panda, IIT Delhi

Sub--Programs:
Sub
g
Motivation
What if we need both DotProduct and
Matrix Multiplication?
Duplication of code
Need way to invoke DotProduct directly

Feb 2016

(C) P. R. Panda, IIT Delhi

Calling
g a Sub
Sub--Program
g
Make DotProduct a Subprogram
DotProduct is CALLED from
MatM lt
MatMult
It can also be called by some
other algorithm
Updates to DotProduct algorithm
done only once

Feb 2016

main()
{
float a[4][4],
a[4][4] b[4][4],
b[4][4] c[4][4];
.../* read values into arrays */
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
c[i][j] = DotProduct (...);
}
}

(C) P. R. Panda, IIT Delhi

Algorithm
Give an Algorithm for
putting
tti an El
Elephant
h t iinto
t
a Refrigerator!

main() {
open_refrigerator_door
...
push_elephant_in
...
close_refrigerator_door
}

Resources (fil
R
(files, d
devices)
i
) often
ft need
d an
open/close (or acquire/release) protocol
Opening process may be non-trivial and
ma stall the ssystem
may
stem (e
(e.g.,
g GPS de
device)
ice)
Feb 2016

(C) P. R. Panda, IIT Delhi

Algorithm: Phase 2
Give an Algorithm for putting a Giraffe into the Refrigerator
Refrigerator.
main() {
open_refrigerator_door
...
pull elephant out
pull_elephant_out
...
push_giraffe_in
...
close_refrigerator_door
}

main() {
open_refrigerator_door
...
if (occupied)
pull_occupant_out
ll
t
t
...
push_giraffe_in
...
close_refrigerator_door
}
Device status needs to be checked first
before using
Someone else may be using it

Feb 2016

(C) P. R. Panda, IIT Delhi

Compare the two Phases


main()
i () { // Phase
Ph
1
open_refrigerator_door
...
push elephant in
push_elephant_in
...
close_refrigerator_door
// Phase 2
open_refrigerator_door
...
if (occupied)
pull_occupant_out
...
push_g
p
giraffe_in
...
close_refrigerator_door
}
Feb 2016

Code is duplicated
Can move duplicate
code to separate
FUNCTION

(C) P. R. Panda, IIT Delhi

replace by Function
Call
10

Modular code with FUNCTIONS


bool is_occupied = FALSE;
open_refrigerator_door () {...}
close_refrigerator_door () {...}
push_elephant_in () {...
is_occupied = TRUE;
}
pull_occupant_out () {...
i
is_occupied
i d = FALSE;
FALSE
}
push_giraffe_in () {...
i
is_occupied
i d = TRUE;
TRUE
}

Feb 2016

main() {
open refrigerator door ();
open_refrigerator_door
push_elephant_in ();
close_refrigerator_door ();
open refrigerator door ();
open_refrigerator_door
if (is_occupied)
pull_occupant_out ();
push_g
p
giraffe_in ();
close_refrigerator_door ();
}

(C) P. R. Panda, IIT Delhi

11

Further Refinement
Pushing an Elephant and Pushing a Giraffe likely
to have some common steps
push_elephant_in () {...
buy_rope_from_market;
tie_neck_of_elephant;
hammer_pulley_into_fridge;
pass_rope_over_pulley;
pull_on_rope;
is_occupied = TRUE;
}

Feb 2016

push_giraffe_in () {...
buy_rope_from_market;
tie_neck_of_giraffe;
hammer_pulley_into_fridge;
pass_rope_over_pulley;
pull_on_rope;
is_occupied = TRUE;
}

(C) P. R. Panda, IIT Delhi

12

Function Parameters

Make the Pushing function generic


able
bl tto handle
h dl ttask
k ffor ANY animal
i l
Function with PARAMETER

main() {
open_refrigerator_door
fi
t d
()
();
push_in (elephant);
close_refrigerator_door ();
open refrigerator door ();
open_refrigerator_door
()
if (is_occupied)
pull_occupant_out ();
push in (giraffe);
push_in
close_refrigerator_door ();
}

bool is_occupied = FALSE


push_in (Animal a) {...
buyy_rope
p _from_market;;
tie_neck_of_a;
hammer_pulley_into_fridge;
pass_rope_over_pulley;
pull_on_rope;
is_occupied = TRUE;
}
open_refrigerator_door () {...}
close_refrigerator_door () {...}
pull_occupant_out () {...
i
is_occupied
i d = FALSE;
FALSE
}

Disclaimer: This is fiction. We dont advocate violence in any form.


Feb 2016

(C) P. R. Panda, IIT Delhi

13

Sub--Programs: Motivation
Sub

Avoiding
g code
duplication
call print_array
function at different
places in program
reduces program
size

One task per function


divide complex task
into several
functions
helps organise and
manage large
programs

Feb 2016

main()
i ()
{
...
for (i = 0; i < 4; i++)
cout << a[i];
for (i = 0; i < 4; i++)
a[i] = a[i] + 1;
for (i = 0; i < 4; i++)
cout << a[i];
}

(C) P. R. Panda, IIT Delhi

print_array () {
for (i = 0; i < 4; i++)
[i]
coutt << a[i];
}
update_array () {
for (i = 0; i < 4; i++)
a[i] = a[i] + 1;
}
int main(){
...
print_array ();
update_array ();
pprint_arrayy ();
}
14

Value Returned by Functions


float dot_prod means,
f
function
ti dot_prod
d t
d RETURNS a
value of type float
This value can then be used in
an expression

Feb 2016

(C) P. R. Panda, IIT Delhi

float a[4], b[4];


fl t dot_prod
d t
d () {
float
float x = 0.0;
for (int i = 0; i < 4; i++)
x += a[i]*b[i];
return x;
}
int main() {
float g;
g = dot_prod () + 1.0;
...
}
15

Procedures: Functions Returning


g
Nothing
Function print_array performs some
specified task
It does not return anything to CALLER
It is called a PROCEDURE

In C++, a Procedure is identified by


the return type
yp void for a function
If return type is void, no value should
be returned

Feb 2016

(C) P. R. Panda, IIT Delhi

void print_array () {
for (i = 0; i < 4; i++)
[i]
coutt << a[i];
}
void update_array () {
for (i = 0; i < 4; i++)
a[i] = a[i] + 1;
}
int main(){
...
print_array ();
update_array ();
pprint_arrayy ();
}
16

Functions vs Procedures
Procedure is a generalisation of a
statement
Function is a generalisation of an
expression
can occur on RHS off assignment
i
t

Feb 2016

(C) P. R. Panda, IIT Delhi

17

You might also like