SlideShare a Scribd company logo
Introduction ttoo PPrrooggrraammmmiinngg 
LLeeccttuurree 99
PPrrooggrraammmmiinngg 
TToooollkkiitt 
 DDeecciissiioonnss 
 LLooooppss 
 SSeeqquueenncceess
LLaabboorraattoorryy SSttooooll
Constructing aa llaabboorraattoorryy 
SSttooooll
Constructing aa llaabboorraattoorryy 
SSttooooll 
 TTaasskk:: MMaakkiinngg aa ssttooooll 
– SSuubbttaasskk:: 
 MMaakkee aa sseeaatt 
 MMaakkee lleeggss ffoorr tthhee ssttooooll 
 AAsssseemmbbllee tthheemm
WWhhaatt wwee wwiillll ssttuuddyy ttooddaayy …… 
 What aarree ffuunnccttiioonnss?? 
 HHooww aarree tthheeyy ddeeffiinneedd ?? 
 HHooww aarree tthheeyy ddeeccllaarreedd ?? 
 WWhhaatt vvaalluueess aarree ppaasssseedd ttoo 
ffuunnccttiioonnss ?? 
 WWhhaatt vvaalluueess ddoo ffuunnccttiioonnss rreettuurrnn ??
FFuunnccttiioonn 
FFuunnccttiioonn nnaammee 
{{ 
BBooddyy ooff tthhee ffuunnccttiioonn 
}}
FFuunnccttiioonn 
TTwwoo ttyyppeess ooff ffuunnccttiioonnss:: 
11.. FFuunnccttiioonnss tthhaatt rreettuurrnn aa vvaalluuee 
22.. FFuunnccttiioonnss tthhaatt ddoo nnoott rreettuurrnn aa vvaalluuee
Function 
return-value-type function-name( aarrgguummeenntt--lliisstt )) 
{{ 
ddeeccllaarraattiioonnss aanndd ssttaatteemmeennttss 
}}
Declaration of 
Function 
return-value-type function-name( aarrgguummeenntt----ttyyppee--lliisstt)) ;; 
mmaaiinn (( )) 
{{ 
:: 
}}
EExxaammppllee 
iinntt ffuunnccttiioonn--nnaammee (( iinntt ,, iinntt ,, ddoouubbllee )) ;; 
vvooiidd mmaaiinn (( )) 
{{ 
…….. 
}}
iinntt ffuunnccttiioonn--nnaammee (( iinntt ii ,, ddoouubbllee jj )) 
{{ 
…… 
}} 
Definition of Function
Return Type of Function 
iinntt ssqquuaarree (( iinntt )) ;; 
iinntt ssqquuaarree (( iinntt ii )) 
{{ 
rreettuurrnn (( ii ** ii )) ;; 
}} 
Declaration 
Definition
Function Call 
iinntt xx ;; 
xx == ssqquuaarree (( ii )) ;;
Example: Function to calculate 
double raiseToPow ( double xx ,, iinntt ppoowweerr )) 
{{ 
ddoouubbllee rreessuulltt ;; 
iinntt ii ;; 
rreessuulltt == 11..00 ;; 
ffoorr (( ii == 11 ;; ii <<== ppoowweerr ;; ii ++++ )) //// bbrraacceess ffiirrsstt 
{{ 
rreessuulltt ** == xx ;; //// rreessuulltt == rreessuulltt **xx 
}} 
rreettuurrnn (( rreessuulltt )) ;; 
}} 
integer power ( Xn )
include << iioossttrreeaamm..hh >> 
vvooiidd mmaaiinn (( )) 
{{ 
ddoouubbllee xx ;; 
iinntt ii ;; 
ccoouutt <<<< ““ PPlleeaassee eenntteerr tthhee nnuummbbeerr ““ ;; 
cciinn >>>> xx ;; 
ccoouutt <<<< ““ PPlleeaassee eenntteerr tthhee iinntteeggeerr ppoowweerr tthhaatt yyoouu wwaanntt tthhiiss nnuummbbeerr rraaiisseedd ttoo ““ cciinn >>>> ii ;; 
ccoouutt <<<< xx <<<< ““ rraaiissee ttoo ppoowweerr ““ <<<< ii <<<< ““iiss eeqquuaall ttoo ““ <<<< rraaiisseeTTooPPooww (( xx ,, ii )) ;; 
}} 
Code to Call the raisetopow 
Function
CCaallll BByy VVaalluuee
CCaalllliinngg ffuunnccttiioonn 
CCaalllleedd ffuunnccttiioonn
AArreeaa ooff tthhee RRiinngg 
Inner Circle 
Outer Circle 
Area of Outer Circle ____ Area of Inner Circle = Area of the Ring
Example: Function to calculate 
the area of a circle 
double cciirrcclleeAArreeaa (( ddoouubbllee rraaddiiuuss )) 
{{ 
rreettuurrnn (( 33..11441155992266 ** rraaddiiuuss ** rraaddiiuuss )) ;; 
}}
Calculating ringArea without 
mmaaiinn (( )) 
{{ 
using Function 
:: 
rriinnggAArreeaa == (( 33..11441155992266 ** rraadd11 ** rraadd11 )) –– (( 33..11441155992266 ** rraadd22 ** rraadd22 )) ;; 
}}
EExxeerrcciisseess 
11.. MMooddiiffyy tthhee rraaiissee ttoo ppoowweerr ffuunnccttiioonn 
ssoo tthhaatt iitt ccaann hhaannddllee nneeggaattiivvee ppoowweerr 
ooff xx,, zzeerroo aanndd ppoossiittiivvee ppoowweerr ooff xx.. 
22.. FFoorr tthhee aarreeaa ooff rriinngg ffuunnccttiioonn ppuutt iinn 
eerrrroorr cchheecckkiinngg mmeecchhaanniissmm..
IInn ttooddaayy’’ss lleeccttuurree 
 We used functions for breaking ccoommpplleexx pprroobblleemmss iinnttoo 
ssmmaalllleerr ppiieecceess,, wwhhiicchh iiss aa ttoopp--ddoowwnn ssttrruuccttuurreedd aapppprrooaacchh.. 
 EEaacchh ffuunnccttiioonn sshhoouulldd bbee aa ssmmaallll mmoodduullee,, sseellff ccoonnttaaiinneedd 
aanndd iitt sshhoouulldd ssoollvvee aa wweellll ddeeffiinneedd pprroobblleemm.. 
 VVaarriiaabbllee nnaammeess aanndd ffuunnccttiioonn nnaammeess sshhoouulldd bbee sseellff 
eexxppllaannaattoorryy.. 
 AAllwwaayyss ccoommmmeenntt yyoouurr ccooddee

More Related Content

PPT
CS201- Introduction to Programming- Lecture 29
PPT
CS201- Introduction to Programming- Lecture 26
PPT
CS201- Introduction to Programming- Lecture 23
PPT
Gráfica y análisis de la función cuadrática
PPT
CS201- Introduction to Programming- Lecture 03
PPT
CS201- Introduction to Programming- Lecture 30
PPT
CS201- Introduction to Programming- Lecture 15
PPT
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 23
Gráfica y análisis de la función cuadrática
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 32

What's hot (20)

PPT
CS201- Introduction to Programming- Lecture 31
PPT
CS201- Introduction to Programming- Lecture 13
PPT
CS201- Introduction to Programming- Lecture 33
PPT
CS201- Introduction to Programming- Lecture 14
PPT
CS201- Introduction to Programming- Lecture 34
PPT
CS201- Introduction to Programming- Lecture 40
PPT
CS201- Introduction to Programming- Lecture 37
PPT
CS201- Introduction to Programming- Lecture 10
PPT
CS201- Introduction to Programming- Lecture 44
PPT
CS201- Introduction to Programming- Lecture 25
PPT
CS201- Introduction to Programming- Lecture 41
PPT
CS201- Introduction to Programming- Lecture 20
PPT
System of linear equation by Graphical Method
PPT
CS201- Introduction to Programming- Lecture 27
PPT
CS201- Introduction to Programming- Lecture 45
PPT
3r ESO: ESTRUCTURA DE LA MATÈRIA
PPT
CS201- Introduction to Programming- Lecture 12
PPT
anatomy of the larynx
PPT
CS201- Introduction to Programming- Lecture 42
PPT
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 13
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 14
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 10
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 20
System of linear equation by Graphical Method
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 45
3r ESO: ESTRUCTURA DE LA MATÈRIA
CS201- Introduction to Programming- Lecture 12
anatomy of the larynx
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 39
Ad

Viewers also liked (19)

DOCX
Poto copian pemahaman studi hukum islam
PPS
ENG101- English Comprehension- Lecture 31
PDF
Aga power softcase catalog
PPTX
Kids Party Decoration Supplies Online
PPTX
IT security - continuïteit van uw onderneming - Orbid
PPS
CS101- Introduction to Computing- Lecture 41
DOCX
Shi pemahaman hukum islam
PPTX
Sejarah teknologi komputer dan internet
PPTX
Botón de mano y volante de mano
DOC
Mtm terbaru 02 06-2014 (2)
DOCX
Bab i, ii, iii
DOCX
Untuk presentasi
PPT
MGT101 - Financial Accounting- Lecture 37
PPS
CS101- Introduction to Computing- Lecture 27
DOCX
K om non verbal
DOCX
Studi hukum islam
PDF
Describing different childrens party themes
DOCX
DOCX
Poto copian pemahaman studi hukum islam
ENG101- English Comprehension- Lecture 31
Aga power softcase catalog
Kids Party Decoration Supplies Online
IT security - continuïteit van uw onderneming - Orbid
CS101- Introduction to Computing- Lecture 41
Shi pemahaman hukum islam
Sejarah teknologi komputer dan internet
Botón de mano y volante de mano
Mtm terbaru 02 06-2014 (2)
Bab i, ii, iii
Untuk presentasi
MGT101 - Financial Accounting- Lecture 37
CS101- Introduction to Computing- Lecture 27
K om non verbal
Studi hukum islam
Describing different childrens party themes
Ad

Similar to CS201- Introduction to Programming- Lecture 09 (18)

PPT
CS201- Introduction to Programming- Lecture 07
PPT
CS201- Introduction to Programming- Lecture 11
PPT
21 High-quality programming code construction part-ii
PPT
CS201- Introduction to Programming- Lecture 38
PPT
CS201- Introduction to Programming- Lecture 06
PPT
CS201- Introduction to Programming- Lecture 18
PPT
CS201- Introduction to Programming- Lecture 36
PPT
Design of the South Doña Ana Dam
PPT
Avaliacao de investimentos
PPT
Tiro parabólico
PPT
CS201- Introduction to Programming- Lecture 28
PDF
TOP Downloaded Papers (January)--International Journal of Computer Networks &...
PPS
Resumen termoquimica1
PPT
Training methods
PPT
Osteo osteomalacia ricket2
PPT
01 termoqu%e dmica
PPT
Government resources
CS201- Introduction to Programming- Lecture 07
CS201- Introduction to Programming- Lecture 11
21 High-quality programming code construction part-ii
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 36
Design of the South Doña Ana Dam
Avaliacao de investimentos
Tiro parabólico
CS201- Introduction to Programming- Lecture 28
TOP Downloaded Papers (January)--International Journal of Computer Networks &...
Resumen termoquimica1
Training methods
Osteo osteomalacia ricket2
01 termoqu%e dmica
Government resources

More from Bilal Ahmed (8)

PPT
CS201- Introduction to Programming- Lecture 43
PPT
CS201- Introduction to Programming- Lecture 35
PPT
CS201- Introduction to Programming- Lecture 24
PPT
CS201- Introduction to Programming- Lecture 22
PPT
CS201- Introduction to Programming- Lecture 21
PPT
CS201- Introduction to Programming- Lecture 19
PPT
CS201- Introduction to Programming- Lecture 17
PPT
CS201- Introduction to Programming- Lecture 16
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 16

CS201- Introduction to Programming- Lecture 09

  • 2. PPrrooggrraammmmiinngg TToooollkkiitt  DDeecciissiioonnss  LLooooppss  SSeeqquueenncceess
  • 5. Constructing aa llaabboorraattoorryy SSttooooll  TTaasskk:: MMaakkiinngg aa ssttooooll – SSuubbttaasskk::  MMaakkee aa sseeaatt  MMaakkee lleeggss ffoorr tthhee ssttooooll  AAsssseemmbbllee tthheemm
  • 6. WWhhaatt wwee wwiillll ssttuuddyy ttooddaayy ……  What aarree ffuunnccttiioonnss??  HHooww aarree tthheeyy ddeeffiinneedd ??  HHooww aarree tthheeyy ddeeccllaarreedd ??  WWhhaatt vvaalluueess aarree ppaasssseedd ttoo ffuunnccttiioonnss ??  WWhhaatt vvaalluueess ddoo ffuunnccttiioonnss rreettuurrnn ??
  • 7. FFuunnccttiioonn FFuunnccttiioonn nnaammee {{ BBooddyy ooff tthhee ffuunnccttiioonn }}
  • 8. FFuunnccttiioonn TTwwoo ttyyppeess ooff ffuunnccttiioonnss:: 11.. FFuunnccttiioonnss tthhaatt rreettuurrnn aa vvaalluuee 22.. FFuunnccttiioonnss tthhaatt ddoo nnoott rreettuurrnn aa vvaalluuee
  • 9. Function return-value-type function-name( aarrgguummeenntt--lliisstt )) {{ ddeeccllaarraattiioonnss aanndd ssttaatteemmeennttss }}
  • 10. Declaration of Function return-value-type function-name( aarrgguummeenntt----ttyyppee--lliisstt)) ;; mmaaiinn (( )) {{ :: }}
  • 11. EExxaammppllee iinntt ffuunnccttiioonn--nnaammee (( iinntt ,, iinntt ,, ddoouubbllee )) ;; vvooiidd mmaaiinn (( )) {{ …….. }}
  • 12. iinntt ffuunnccttiioonn--nnaammee (( iinntt ii ,, ddoouubbllee jj )) {{ …… }} Definition of Function
  • 13. Return Type of Function iinntt ssqquuaarree (( iinntt )) ;; iinntt ssqquuaarree (( iinntt ii )) {{ rreettuurrnn (( ii ** ii )) ;; }} Declaration Definition
  • 14. Function Call iinntt xx ;; xx == ssqquuaarree (( ii )) ;;
  • 15. Example: Function to calculate double raiseToPow ( double xx ,, iinntt ppoowweerr )) {{ ddoouubbllee rreessuulltt ;; iinntt ii ;; rreessuulltt == 11..00 ;; ffoorr (( ii == 11 ;; ii <<== ppoowweerr ;; ii ++++ )) //// bbrraacceess ffiirrsstt {{ rreessuulltt ** == xx ;; //// rreessuulltt == rreessuulltt **xx }} rreettuurrnn (( rreessuulltt )) ;; }} integer power ( Xn )
  • 16. include << iioossttrreeaamm..hh >> vvooiidd mmaaiinn (( )) {{ ddoouubbllee xx ;; iinntt ii ;; ccoouutt <<<< ““ PPlleeaassee eenntteerr tthhee nnuummbbeerr ““ ;; cciinn >>>> xx ;; ccoouutt <<<< ““ PPlleeaassee eenntteerr tthhee iinntteeggeerr ppoowweerr tthhaatt yyoouu wwaanntt tthhiiss nnuummbbeerr rraaiisseedd ttoo ““ cciinn >>>> ii ;; ccoouutt <<<< xx <<<< ““ rraaiissee ttoo ppoowweerr ““ <<<< ii <<<< ““iiss eeqquuaall ttoo ““ <<<< rraaiisseeTTooPPooww (( xx ,, ii )) ;; }} Code to Call the raisetopow Function
  • 19. AArreeaa ooff tthhee RRiinngg Inner Circle Outer Circle Area of Outer Circle ____ Area of Inner Circle = Area of the Ring
  • 20. Example: Function to calculate the area of a circle double cciirrcclleeAArreeaa (( ddoouubbllee rraaddiiuuss )) {{ rreettuurrnn (( 33..11441155992266 ** rraaddiiuuss ** rraaddiiuuss )) ;; }}
  • 21. Calculating ringArea without mmaaiinn (( )) {{ using Function :: rriinnggAArreeaa == (( 33..11441155992266 ** rraadd11 ** rraadd11 )) –– (( 33..11441155992266 ** rraadd22 ** rraadd22 )) ;; }}
  • 22. EExxeerrcciisseess 11.. MMooddiiffyy tthhee rraaiissee ttoo ppoowweerr ffuunnccttiioonn ssoo tthhaatt iitt ccaann hhaannddllee nneeggaattiivvee ppoowweerr ooff xx,, zzeerroo aanndd ppoossiittiivvee ppoowweerr ooff xx.. 22.. FFoorr tthhee aarreeaa ooff rriinngg ffuunnccttiioonn ppuutt iinn eerrrroorr cchheecckkiinngg mmeecchhaanniissmm..
  • 23. IInn ttooddaayy’’ss lleeccttuurree  We used functions for breaking ccoommpplleexx pprroobblleemmss iinnttoo ssmmaalllleerr ppiieecceess,, wwhhiicchh iiss aa ttoopp--ddoowwnn ssttrruuccttuurreedd aapppprrooaacchh..  EEaacchh ffuunnccttiioonn sshhoouulldd bbee aa ssmmaallll mmoodduullee,, sseellff ccoonnttaaiinneedd aanndd iitt sshhoouulldd ssoollvvee aa wweellll ddeeffiinneedd pprroobblleemm..  VVaarriiaabbllee nnaammeess aanndd ffuunnccttiioonn nnaammeess sshhoouulldd bbee sseellff eexxppllaannaattoorryy..  AAllwwaayyss ccoommmmeenntt yyoouurr ccooddee