Professional Documents
Culture Documents
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
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
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
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
Feb 2016
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
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
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 (...);
}
}
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
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
Code is duplicated
Can move duplicate
code to separate
FUNCTION
replace by Function
Call
10
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 ();
}
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;
}
12
Function Parameters
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 ();
}
13
Sub--Programs: Motivation
Sub
Avoiding
g code
duplication
call print_array
function at different
places in program
reduces program
size
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];
}
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
Feb 2016
Feb 2016
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
17