Stubs and Drivers

advertisement

Stubs and Drivers

Stubs and Drivers

 Functions are useful for top-down design

 break a big problem down into smaller pieces

the top level

def main (): a1 = 5 a2 = 33 a3 = a2+5 partone(a1, a2) a2 = a2 + parttwo (a1, a3) partThree (a1, a2, a3, 23) main()

but how to run?

 The program in the previous slide won't run until all the other functions have definitions

 If you just want to write one of them, you have to fill in the others with "dummies" =

"stubs"

 A stub has the right header but doesn't do anything useful towards solving the problem

two Stubs

def partThree (p1, p2, p3, p4): print("in the partThree function") print( p1, p2 , p3) def partTwo (p1, p2): print ("in partTwo") return 1

going the other way

 suppose you have written a function that solves a problem, but...

 you don't have the main function written yet

 how can you test your function without it?

 write a "driver"

Driver

 Usually a main function

 just calls the function to be tested with arguments that make sense

 reports the results

 does not try to solve the whole problem

Example Driver

def main (): x = 5 z= 23 print ("calling fun1 with x =", x, " and z = ", z) y = fun1 (x, z) print( "result of calling fun1 is ", y)

scaffolding

 both stubs and drivers known as

"scaffolding"

 not part of the finished program but very useful when testing parts of the program

 usually removed when program is finished

Download