Sei sulla pagina 1di 30

CS101ComputerProgramming

andUtilization
Lecture9
Separatecompilationoffunctions
DecisionTrees
Feb8,2011
Prof.RKJoshi
ComputerScienceandEngineering
IITBombay

Email:rkj@cse.iitb.ac.in

Revision:Functions

Givingnamestocodesegments
Aunitofreuse,defineonce,ensure
correctness,useagainandagain

Functionsandprocedures

Functionslikemathematicalfunctions

Inputs

Anoutput

Nohistoryremembered

Functiondeclaration

Alsocalled'signature'

Name,inputtypes,output
type

Functiondefinition:body

Functioninvocation(call)

Sendintheactual
parameters

Obtainareturnvalue

Controltransfersto
thefunction

Callerblockstillafunction
callcompletes
Whenacallreturns,the
controlistransferredbackto
caller

SeparateCompilation

Whatifyouwantedtouseafunctiondefinedin
yourprograminsomeotherprogram?
Youwillhavetocopythecodeintheprogram
andthenusethefunctioncall
Thiscanbeavoidedbywritingthefunctionina
separatefile
Thefilecanbelinkedwithyourprogram

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Definition
Sourcefile

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Definition
Sourcefile

compile

Compiled
code

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Definition
Sourcefile

Function
Usedinthis
Sourcefile

compile

Compiled
code

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Definition
Sourcefile

Function
Usedinthis
Sourcefile

compile
compile

Compiled
code

Compiled
code

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Usedinthis
Sourcefile

Function
Definition
Sourcefile

compile
compile
link
Compiled
code

Compiled
code

Separatingfunctiondefinitionfromitsuse,and
compilingthemseparately

Function
Usedinthis
Sourcefile

Function
Definition
Sourcefile

compile
compile
link
Compiled
code

Compiled
code

Executablecode
a.out

DecisionTrees
Firstsecondfirstnumber
Wheneveranextnumber'n'comesup..

Whatwouldyoudo?

DecisionTree
first==second?

DecisionTree
first==second?
Yes

DecisionTree
first==second?
Yes
nis
higher

DecisionTree
first==second?
Yes
nisnis
higherlower

DecisionTree
first==second?
Yes
nisnisnissame
higherlowerasboth

DecisionTree
first==second?
Yes
nisnisnissame
higherlowerasboth
firstn

DecisionTree
first==second?
Yes
nisnisnissame
higherlowerasboth
firstnsecondn

DecisionTree
first==second?
Yes
nisnisnissame
higherlowerasboth
firstnsecondndon'tbother

DecisionTree
first==second?
YesNo

DecisionTree
first==second?
YesNo
nin
between

DecisionTree
first==second?
YesNo
ninnhigher
betweenthanfirst

DecisionTree
first==second?
YesNo
ninnhighernsmaller
betweenthanfirstthansecond

DecisionTree
first==second?
YesNo
ninnhighernsmaller
betweenthanfirstthansecond

Secondn

DecisionTree
first==second?
YesNo
ninnhighernsmaller
betweenthanfirstthansecond

Secondnsecondfirst
Firstn

DecisionTree
first==second?
YesNo
ninnhighernsmaller
betweenthanfirstthansecond

Secondnsecondfirstdon't
Firstnbother

DecisionTree
first==second?
YesNo
ninnhighernsmaller
betweenthanfirstthansecond

Secondnsecondfirstdon't
Firstnbother
Anycaseleftout?

DecisionTree

first==second?

No

ninnhighernsmallernsameasnsame

betweenthanfirstthansecondfirstassecond

Secondnsecondfirstdon'tdon'tdon't

Firstnbotherbotherbother

Anothertree

n>first
YesNo

Anothertree..

n>first
YesNo
n=first
first<n>second
n=second
n<second

Completethis...

Yetanothertree

N<second?
Yesno

Howwillyougrowthisone?

Potrebbero piacerti anche