SlideShare a Scribd company logo
Introduction ttoo PPrrooggrraammmmiinngg 
LLeeccttuurree 1100
TTooddaayy''ss LLeeccttuurree IInncclluuddeess 
 HHeeaaddeerr FFiilleess 
 SSccooppee ooff VVaarriiaabblleess 
 FFuunnccttiioonnss 
– CCaallll bbyy vvaalluuee 
– CCaallll bbyy rreeffeerreennccee
HHeeaaddeerr FFiilleess 
##iinncclluuddee <<iioossttrreeaamm..hh>>
PPrroottoottyyppee 
Return value Assignment List 
with data type 
iinntt ffuunnccttiioonnNNaammee ( iinntt ,, iinntt ));;
UUssiinngg HHeeaaddeerr FFiilleess 
ddoouubbllee ppii == 33..11441155992266;; 
 IItt iiss bbeetttteerr ttoo ddeeffiinnee tthhiiss vvaalluuee iinn aa hheeaaddeerr ffiillee 
 TThheenn ssiimmppllyy bbyy iinncclluuddiinngg tthhee hheeaaddeerr ffiillee iinn tthhee 
pprrooggrraamm tthhiiss vvaalluuee iiss ddeeffiinneedd aanndd iitt hhaass aa 
mmeeaanniinnggffuull nnaammee
#define 
 ##ddeeffiinnee ppii 33..11441155992266 
 NNaammee ccaann bbee uusseedd iinnssiiddee aa pprrooggrraamm 
eexxaaccttllyy lliikkee aa vvaarriiaabbllee 
 IItt ccaannnnoott bbee uusseedd aass aa vvaarriiaabbllee 
CCiirrcclleeAArreeaa == ppii ** rraaddiiuuss ** rraaddiiuuss 
CCiirrccuummffeerreennccee == 22 ** ppii ** rraaddiiuuss
SSccooppee ooff IIddeennttiiffiieerrss 
 Identifier is aannyy nnaammee uusseerr ccrreeaatteess iinn 
hhiiss//hheerr pprrooggrraamm 
 FFuunnccttiioonnss aarree aallssoo iiddeennttiiffiieerrss 
 LLaabbeellss aarree aallssoo iiddeennttiiffiieerrss
SSccooppee ooff IIddeennttiiffiieerrss 
 SSccooppee mmeeaannss vviissiibbiilliittyy 
 AA vvaarriiaabbllee ddeeccllaarreedd iinnssiiddee aa bblloocckk hhaass 
vviissiibbiilliittyy wwiitthhiinn tthhaatt bblloocckk oonnllyy 
 VVaarriiaabblleess ddeeffiinneedd wwiitthhiinn tthhee ffuunnccttiioonn 
hhaass aa ssccooppee tthhaatt iiss ffuunnccttiioonn wwiiddee
EExxaammppllee 
vvooiidd ffuunnccttiioonnNNaammee (( )) 
{{ 
{{ 
iinntt ii ;; 
}} 
…….... 
}}
IIddeennttiiffiieerrss IImmppoorrttaanntt PPooiinnttss 
 DDoo nnoott ccrreeaattee vvaarriiaabblleess wwiitthh ssaammee 
nnaammee iinnssiiddee bblloocckkss,, iinnssiiddee ffuunnccttiioonnss 
oorr iinnssiiddee bbiiggggeerr bblloocckkss 
 TTrryy ttoo uussee sseeppaarraattee vvaarriiaabbllee nnaammeess 
ttoo aavvooiidd ccoonnffuussiioonn 
 RReeuussee ooff vvaarriiaabblleess iiss vvaalliidd
FFiillee SSccooppee 
## iinncclluuddee << iioossttrreeaamm..hh >> 
iinntt ii ;; 
Global variable
GGlloobbaall VVaarriiaabbllee 
 CCaann bbee uusseedd aannyywwhheerree iinn pprrooggrraamm 
 CCaann ccaauussee llooggiiccaall pprroobblleemmss iiff ssaammee vvaarriiaabbllee 
nnaammee iiss uusseedd iinn llooccaall vvaarriiaabbllee ddeeccllaarraattiioonnss 
FFoorr ggoooodd pprrooggrraammmmiinngg 
 TTrryy ttoo mmiinniimmiizzee tthhee uussee ooff gglloobbaall vvaarriiaabblleess 
 TTrryy ttoo uussee llooccaall vvaarriiaabblleess aass ffaarr aass ppoossssiibbllee
VViissiibbiilliittyy ooff IIddeennttiiffiieerrss 
 GGlloobbaall SSccooppee 
AAnnyytthhiinngg iiddeennttiiffiieedd oorr ddeeccllaarreedd oouuttssiiddee ooff 
aannyy ffuunnccttiioonn iiss vviissiibbllee ttoo aallll ffuunnccttiioonnss iinn tthhaatt 
ffiillee 
 FFuunnccttiioonn lleevveell ssccooppee 
DDeeccllaarriinngg vvaarriiaabblleess iinnssiiddee aa ffuunnccttiioonn ccaann bbee 
uusseedd iinn tthhee wwhhoollee ffuunnccttiioonn 
 BBlloocckk lleevveell ssccooppee 
VVaarriiaabblleess oorr iinntteeggeerrss ddeeccllaarreedd iinnssiiddee bblloocckk 
aarree uusseedd iinnssiiddee bblloocckk
EExxaammppllee: BBlloocckk SSccooppee 
ffoorr (( iinntt ii == 00 ;; ii << 1100 ;; ii++++ )) 
 IItt iiss bblloocckk lleevveell ssccooppee ddeeccllaarreedd iinn ffoorr 
lloooopp 
 WWhheenn ffoorr iiss ffiinniisshheedd ““ ii ”” nnoo lloonnggeerr 
eexxiissttss
EExxaammppllee: GGlloobbaall SSccooppee 
##iinncclluuddee << iioossttrreeaamm..hh >> 
iinntt ii ;; 
vvooiidd ff (( vvooiidd )) ;; 
mmaaiinn (( )) 
{{ 
ii == 1100 ;; 
ccoouutt<<<< ““ wwiitthhiinn mmaaiinn ii == ““ <<<< ii ;; 
ff (( )) ;; 
}}
EExxaammppllee: GGlloobbaall SSccooppee 
vvooiidd ff (( vvooiidd )) 
{{ 
ccoouutt<<<< ““ IInnssiiddee ffuunnccttiioonn ff ,, ii ==““ <<<< ii ;; 
ii == 2200 ;; 
}}
EExxaammppllee:: CCaallll bbyy VVaalluuee 
##iinncclluuddee <<iioossttrreeaamm..hh >> 
iinntt ff (( iinntt )) ;; 
mmaaiinn (( )) 
{{ 
iinntt ii == 1100 ;; 
ccoouutt <<<< ““IInn mmaaiinn ii == "" <<<< ii ;; 
ff (( ii )) ;; 
ccoouutt <<<< "" BBaacckk iinn mmaaiinn,, ii == "" <<<< ii ;; 
s 
}}
iinntt ff (( iinntt ii )) 
{{ 
ccoouutt <<<< ""IInn ffuunnccttiioonn ff ,, ii == "" <<<< ii ;; 
ii **== 22 ;; 
ccoouutt <<<< ""IInn ffuunnccttiioonn ff ,, ii iiss nnooww == ““ <<<< ii ;; 
rreettuurrnn ii ;; 
}} 
EExxaammppllee:: CCaallll bbyy 
VVaalluuee
double square (( ddoouubbllee xx )) 
{{ 
rreettuurrnn xx ** xx ;; 
}} 
mmaaiinn (( )) 
{{ 
ddoouubbllee nnuummbbeerr == 112233..445566 ;; 
ccoouutt <<<< ““ TThhee ssqquuaarree ooff ““ <<<< nnuummbbeerr <<<< ““ iiss ““<<<< ssqquuaarree (( nnuummbbeerr )) ;; 
ccoouutt <<<< ““ TThhee ccuurrrreenntt vvaalluuee ooff ““ <<<< nnuummbbeerr <<<< ““iiss ““ <<<< nnuummbbeerr ;; 
}} 
EExxaammppllee :: SSqquuaarree ooff aa 
NNuummbbeerr
MMaatthh..hh 
##iinncclluuddee << mmaatthh..hh >> 
ddoouubbllee ssqqrrtt (( ddoouubbllee ));; 
lloogg1100 ,, ppooww (( xxyy )) ,, ssiinn ,, ccooss ,, ttaann ……
CCaallll bbyy RReeffeerreennccee 
 AA ffuunnccttiioonn iinn wwhhiicchh oorriiggiinnaall vvaalluuee ooff tthhee 
vvaarriiaabbllee iiss cchhaannggeedd 
 TToo ccaallll bbyy rreeffeerreennccee wwee ccaannnnoott ppaassss 
vvaalluuee,, wwee hhaavvee ttoo ppaassss mmeemmoorryy aaddddrreessss 
ooff vvaarriiaabbllee 
 ““&&”” iiss uusseedd ttoo ttaakkee tthhee aaddddrreessss ooff aa 
vvaarriiaabbllee
EExxaammppllee:: CCaallll bbyy 
RReeffeerreennccee 
mmaaiinn (( )) 
{{ 
ddoouubbllee xx == 112233..445566 ;; 
ssqquuaarree (( &&xx )) ;; 
}} 
VVaalluuee ooff ‘‘xx’’ iiss nnoott ppaasssseedd ,, bbuutt tthhee 
mmeemmoorryy aaddddrreessss ooff ‘‘xx’’ iiss ppaasssseedd
EExxaammppllee:: CCaallll bbyy 
RReeffeerreennccee 
x is a pointer to a variable double 
ssqquuaarree (( ddoouubbllee **xx )) 
{{ 
**xx == **xx ** **xx ;; 
}}
PPooiinntteerrss 
 Pointers aarree uusseedd ttoo ppaassss aaddddrreessss ooff 
vvaarriiaabbllee ffoorr rreeffeerreennccee 
 WWee uussee ““ &&xx ”” ttoo sseenndd tthhee aaddddrreessss ooff ““ 
xx ““ 
 TToo rreecceeiivvee tthhee aaddddrreessss wwee uussee ““ **xx ”” 
((wwhhaatteevveerr ““ xx ”” ppooiinnttss ttoo))
RReeccuurrssiivvee 
FFuunnccttiioonnss 
 Special function wwhhiicchh ccaann ccaallll iittsseellff 
xx1100 == xx ** xx99 
xx99 == xx ** xx88 
xx88 == xx ** xx77 
…… …… 
xxnn == xx ** xxnn--11
RReeccuurrssiivvee FFuunnccttiioonnss:: 
FFaaccttoorriiaall 
nn!! == nn ** ((nn--11)) ** ((nn--22)) ………….... 33 ** 22 ** 11 
55!! == 55 ** 44 ** 33 ** 22 ** 11 
44!! == 44 ** 33 ** 22 ** 11 
55!! == 55 ** 44!! 
00!! == 11
RReeccuurrssiivvee FFuunnccttiioonnss:: FFaaccttoorriiaall 
lloonngg ffaaccttoorriiaall (( lloonngg nn )) 
{{ 
iiff ((nn ==== 11 )) 
rreettuurrnn (( nn )) ;; 
eellssee 
rreettuurrnn (( nn ** ffaaccttoorriiaall ((nn--11)) )) ;; 
}}
EExxeerrcciissee 
TTrryy ttoo wwrriittee pprrooggrraamm ffoorr 
 FFiibboonnaaccccii sseerriieess 
 FFiinndd ‘‘ppoowweerr ooff nnuummbbeerr’’ uussiinngg rreeccuurrssiivvee 
tteecchhnniiqquuee
EExxaammppllee 
TThhee FFiibboonnaaccccii SSeerriieess 
 SSeett ooff rreeccuurrssiivvee ccaallllss ttoo ffuunnccttiioonn 
ffiibboonnaaccccii 
f( 3 ) 
return + 
f( 2 ) f( 1 ) 
return + 
f( 1 ) f( 0 ) return 1 
return 1 return 0
MMaannaaggeemmeenntt IIssssuueess ooff 
CCoommppuutteerr 
TThheerree aarree ttwwoo iissssuueess iinnssiiddee aa ccoommppuutteerr 
 MMeemmoorryy oovveerrhheeaadd 
 SSttaacckk oovveerrhheeaadd
PPrrooggrraammmmiinngg OOppttiioonnss 
 EElleeggaanntt ccooddee 
wwhheerree pprriiccee iiss nnoott ttoooo hhiigghh 
 EEffffiicciieenntt ccooddee 
wwhheerree pprriiccee iiss ttoooo hhiigghh
WWhhaatt hhaavvee wwee DDoonnee TTooddaayy …… 
 HHeeaaddeerr FFiilleess 
– NNiiccee mmeecchhaanniissmm ooff ppuuttttiinngg aallll pprroottoottyyppeess 
aanndd ddeeffiinniittiioonnss ooff gglloobbaall ccoonnssttaannttss eettcc.. 
 SSccooppee ooff vvaarriiaabblleess 
 FFuunnccttiioonnss 
– CCaallll bbyy vvaalluuee 
– CCaallll bbyy rreeffeerreennccee 
 RReeccuurrssiioonn

More Related Content

PPT
CS201- Introduction to Programming- Lecture 07
PPT
Fluid & electrolytes & acid base
PPT
CS201- Introduction to Programming- Lecture 15
PPT
CS201- Introduction to Programming- Lecture 40
PPT
Colonial America
PPT
A igreja, corpo de cristo
PPT
Chapter 6 testbench
PPT
Avaliacao de investimentos
CS201- Introduction to Programming- Lecture 07
Fluid & electrolytes & acid base
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 40
Colonial America
A igreja, corpo de cristo
Chapter 6 testbench
Avaliacao de investimentos

What's hot (20)

PPT
cardiac arrhythmias
PPS
Comandos para diseño de página Web
PPT
Edit data base menggunakan web
PPT
Upper tibial valgus osteotomy using a dynamic external fixator
PPT
The anatomy of a dollar bill
PPT
CS201- Introduction to Programming- Lecture 30
PPT
CS201- Introduction to Programming- Lecture 12
PPT
CS201- Introduction to Programming- Lecture 03
PPT
Ancient israel
PPT
Le Black's Law Dictionary, monument de la lexicographie juridique - ATA 2010
PPT
Acceleration and force 2010
DOCX
4th grade, lesson plan
PPT
Symmetry and group theory
PPT
Sexually transmitted infections
PPT
CS201- Introduction to Programming- Lecture 24
PPT
Tiro parabólico
PPT
Estados hipertensivos del embarazo
PPS
Resumen termoquimica1
PPT
Design of the South Doña Ana Dam
PPT
Creative commons and the ethical use of images in language instruction shelton
cardiac arrhythmias
Comandos para diseño de página Web
Edit data base menggunakan web
Upper tibial valgus osteotomy using a dynamic external fixator
The anatomy of a dollar bill
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 03
Ancient israel
Le Black's Law Dictionary, monument de la lexicographie juridique - ATA 2010
Acceleration and force 2010
4th grade, lesson plan
Symmetry and group theory
Sexually transmitted infections
CS201- Introduction to Programming- Lecture 24
Tiro parabólico
Estados hipertensivos del embarazo
Resumen termoquimica1
Design of the South Doña Ana Dam
Creative commons and the ethical use of images in language instruction shelton
Ad

Viewers also liked (20)

PPT
CS201- Introduction to Programming- Lecture 34
PPS
CS101- Introduction to Computing- Lecture 43
DOC
MTH101 - Calculus and Analytical Geometry- Lecture 44
PPS
ENG101- English Comprehension- Lecture 35
PPS
CS101- Introduction to Computing- Lecture 32
PPS
CS101- Introduction to Computing- Lecture 29
PPT
CS201- Introduction to Programming- Lecture 06
PPT
CS201- Introduction to Programming- Lecture 16
PPS
CS101- Introduction to Computing- Lecture 35
PPT
CS201- Introduction to Programming- Lecture 19
PPT
MGT101 - Financial Accounting- Lecture 38
PPT
CS201- Introduction to Programming- Lecture 39
PPS
ENG101- English Comprehension- Lecture 43
PPS
ENG101- English Comprehension- Lecture 42
PPT
CS201- Introduction to Programming- Lecture 08
PPT
CS201- Introduction to Programming- Lecture 43
PPT
MGT101 - Financial Accounting- Lecture 40
PPS
CS101- Introduction to Computing- Lecture 44
PPS
CS101- Introduction to Computing- Lecture 23
PPT
MGT101 - Financial Accounting- Lecture 30
CS201- Introduction to Programming- Lecture 34
CS101- Introduction to Computing- Lecture 43
MTH101 - Calculus and Analytical Geometry- Lecture 44
ENG101- English Comprehension- Lecture 35
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 29
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 16
CS101- Introduction to Computing- Lecture 35
CS201- Introduction to Programming- Lecture 19
MGT101 - Financial Accounting- Lecture 38
CS201- Introduction to Programming- Lecture 39
ENG101- English Comprehension- Lecture 43
ENG101- English Comprehension- Lecture 42
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 43
MGT101 - Financial Accounting- Lecture 40
CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 23
MGT101 - Financial Accounting- Lecture 30
Ad

Similar to CS201- Introduction to Programming- Lecture 10 (20)

PPT
21 High-quality programming code construction part-ii
PPT
Business plan presentation_format
PPT
Spinal injuries
PPT
18 Hash tables and sets
PPT
Training methods
PPT
19 Algorithms and complexity
PPT
solar activity and climate
PPT
13089861
PPT
Solving linear equations (chapter 2)
PPT
Justins turlip soil moisture REUcon
PPT
Source control system
PPT
06.introduction to middle third fractures
PPT
V wd soff 09-11-14
PPT
Corporation
PPT
CS201- Introduction to Programming- Lecture 36
PPT
CS201- Introduction to Programming- Lecture 38
PPT
Building types lecture
PPT
Dielectrics lect28
PPT
Securing PHP Applications
PPT
Call Centre Training Package
21 High-quality programming code construction part-ii
Business plan presentation_format
Spinal injuries
18 Hash tables and sets
Training methods
19 Algorithms and complexity
solar activity and climate
13089861
Solving linear equations (chapter 2)
Justins turlip soil moisture REUcon
Source control system
06.introduction to middle third fractures
V wd soff 09-11-14
Corporation
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 38
Building types lecture
Dielectrics lect28
Securing PHP Applications
Call Centre Training Package

More from Bilal Ahmed (20)

PPT
CS201- Introduction to Programming- Lecture 45
PPT
CS201- Introduction to Programming- Lecture 44
PPT
CS201- Introduction to Programming- Lecture 42
PPT
CS201- Introduction to Programming- Lecture 41
PPT
CS201- Introduction to Programming- Lecture 37
PPT
CS201- Introduction to Programming- Lecture 35
PPT
CS201- Introduction to Programming- Lecture 33
PPT
CS201- Introduction to Programming- Lecture 32
PPT
CS201- Introduction to Programming- Lecture 31
PPT
CS201- Introduction to Programming- Lecture 29
PPT
CS201- Introduction to Programming- Lecture 28
PPT
CS201- Introduction to Programming- Lecture 27
PPT
CS201- Introduction to Programming- Lecture 26
PPT
CS201- Introduction to Programming- Lecture 25
PPT
CS201- Introduction to Programming- Lecture 23
PPT
CS201- Introduction to Programming- Lecture 22
PPT
CS201- Introduction to Programming- Lecture 21
PPT
CS201- Introduction to Programming- Lecture 20
PPT
CS201- Introduction to Programming- Lecture 18
PPT
CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 17

CS201- Introduction to Programming- Lecture 10

  • 2. TTooddaayy''ss LLeeccttuurree IInncclluuddeess  HHeeaaddeerr FFiilleess  SSccooppee ooff VVaarriiaabblleess  FFuunnccttiioonnss – CCaallll bbyy vvaalluuee – CCaallll bbyy rreeffeerreennccee
  • 3. HHeeaaddeerr FFiilleess ##iinncclluuddee <<iioossttrreeaamm..hh>>
  • 4. PPrroottoottyyppee Return value Assignment List with data type iinntt ffuunnccttiioonnNNaammee ( iinntt ,, iinntt ));;
  • 5. UUssiinngg HHeeaaddeerr FFiilleess ddoouubbllee ppii == 33..11441155992266;;  IItt iiss bbeetttteerr ttoo ddeeffiinnee tthhiiss vvaalluuee iinn aa hheeaaddeerr ffiillee  TThheenn ssiimmppllyy bbyy iinncclluuddiinngg tthhee hheeaaddeerr ffiillee iinn tthhee pprrooggrraamm tthhiiss vvaalluuee iiss ddeeffiinneedd aanndd iitt hhaass aa mmeeaanniinnggffuull nnaammee
  • 6. #define  ##ddeeffiinnee ppii 33..11441155992266  NNaammee ccaann bbee uusseedd iinnssiiddee aa pprrooggrraamm eexxaaccttllyy lliikkee aa vvaarriiaabbllee  IItt ccaannnnoott bbee uusseedd aass aa vvaarriiaabbllee CCiirrcclleeAArreeaa == ppii ** rraaddiiuuss ** rraaddiiuuss CCiirrccuummffeerreennccee == 22 ** ppii ** rraaddiiuuss
  • 7. SSccooppee ooff IIddeennttiiffiieerrss  Identifier is aannyy nnaammee uusseerr ccrreeaatteess iinn hhiiss//hheerr pprrooggrraamm  FFuunnccttiioonnss aarree aallssoo iiddeennttiiffiieerrss  LLaabbeellss aarree aallssoo iiddeennttiiffiieerrss
  • 8. SSccooppee ooff IIddeennttiiffiieerrss  SSccooppee mmeeaannss vviissiibbiilliittyy  AA vvaarriiaabbllee ddeeccllaarreedd iinnssiiddee aa bblloocckk hhaass vviissiibbiilliittyy wwiitthhiinn tthhaatt bblloocckk oonnllyy  VVaarriiaabblleess ddeeffiinneedd wwiitthhiinn tthhee ffuunnccttiioonn hhaass aa ssccooppee tthhaatt iiss ffuunnccttiioonn wwiiddee
  • 9. EExxaammppllee vvooiidd ffuunnccttiioonnNNaammee (( )) {{ {{ iinntt ii ;; }} …….... }}
  • 10. IIddeennttiiffiieerrss IImmppoorrttaanntt PPooiinnttss  DDoo nnoott ccrreeaattee vvaarriiaabblleess wwiitthh ssaammee nnaammee iinnssiiddee bblloocckkss,, iinnssiiddee ffuunnccttiioonnss oorr iinnssiiddee bbiiggggeerr bblloocckkss  TTrryy ttoo uussee sseeppaarraattee vvaarriiaabbllee nnaammeess ttoo aavvooiidd ccoonnffuussiioonn  RReeuussee ooff vvaarriiaabblleess iiss vvaalliidd
  • 11. FFiillee SSccooppee ## iinncclluuddee << iioossttrreeaamm..hh >> iinntt ii ;; Global variable
  • 12. GGlloobbaall VVaarriiaabbllee  CCaann bbee uusseedd aannyywwhheerree iinn pprrooggrraamm  CCaann ccaauussee llooggiiccaall pprroobblleemmss iiff ssaammee vvaarriiaabbllee nnaammee iiss uusseedd iinn llooccaall vvaarriiaabbllee ddeeccllaarraattiioonnss FFoorr ggoooodd pprrooggrraammmmiinngg  TTrryy ttoo mmiinniimmiizzee tthhee uussee ooff gglloobbaall vvaarriiaabblleess  TTrryy ttoo uussee llooccaall vvaarriiaabblleess aass ffaarr aass ppoossssiibbllee
  • 13. VViissiibbiilliittyy ooff IIddeennttiiffiieerrss  GGlloobbaall SSccooppee AAnnyytthhiinngg iiddeennttiiffiieedd oorr ddeeccllaarreedd oouuttssiiddee ooff aannyy ffuunnccttiioonn iiss vviissiibbllee ttoo aallll ffuunnccttiioonnss iinn tthhaatt ffiillee  FFuunnccttiioonn lleevveell ssccooppee DDeeccllaarriinngg vvaarriiaabblleess iinnssiiddee aa ffuunnccttiioonn ccaann bbee uusseedd iinn tthhee wwhhoollee ffuunnccttiioonn  BBlloocckk lleevveell ssccooppee VVaarriiaabblleess oorr iinntteeggeerrss ddeeccllaarreedd iinnssiiddee bblloocckk aarree uusseedd iinnssiiddee bblloocckk
  • 14. EExxaammppllee: BBlloocckk SSccooppee ffoorr (( iinntt ii == 00 ;; ii << 1100 ;; ii++++ ))  IItt iiss bblloocckk lleevveell ssccooppee ddeeccllaarreedd iinn ffoorr lloooopp  WWhheenn ffoorr iiss ffiinniisshheedd ““ ii ”” nnoo lloonnggeerr eexxiissttss
  • 15. EExxaammppllee: GGlloobbaall SSccooppee ##iinncclluuddee << iioossttrreeaamm..hh >> iinntt ii ;; vvooiidd ff (( vvooiidd )) ;; mmaaiinn (( )) {{ ii == 1100 ;; ccoouutt<<<< ““ wwiitthhiinn mmaaiinn ii == ““ <<<< ii ;; ff (( )) ;; }}
  • 16. EExxaammppllee: GGlloobbaall SSccooppee vvooiidd ff (( vvooiidd )) {{ ccoouutt<<<< ““ IInnssiiddee ffuunnccttiioonn ff ,, ii ==““ <<<< ii ;; ii == 2200 ;; }}
  • 17. EExxaammppllee:: CCaallll bbyy VVaalluuee ##iinncclluuddee <<iioossttrreeaamm..hh >> iinntt ff (( iinntt )) ;; mmaaiinn (( )) {{ iinntt ii == 1100 ;; ccoouutt <<<< ““IInn mmaaiinn ii == "" <<<< ii ;; ff (( ii )) ;; ccoouutt <<<< "" BBaacckk iinn mmaaiinn,, ii == "" <<<< ii ;; s }}
  • 18. iinntt ff (( iinntt ii )) {{ ccoouutt <<<< ""IInn ffuunnccttiioonn ff ,, ii == "" <<<< ii ;; ii **== 22 ;; ccoouutt <<<< ""IInn ffuunnccttiioonn ff ,, ii iiss nnooww == ““ <<<< ii ;; rreettuurrnn ii ;; }} EExxaammppllee:: CCaallll bbyy VVaalluuee
  • 19. double square (( ddoouubbllee xx )) {{ rreettuurrnn xx ** xx ;; }} mmaaiinn (( )) {{ ddoouubbllee nnuummbbeerr == 112233..445566 ;; ccoouutt <<<< ““ TThhee ssqquuaarree ooff ““ <<<< nnuummbbeerr <<<< ““ iiss ““<<<< ssqquuaarree (( nnuummbbeerr )) ;; ccoouutt <<<< ““ TThhee ccuurrrreenntt vvaalluuee ooff ““ <<<< nnuummbbeerr <<<< ““iiss ““ <<<< nnuummbbeerr ;; }} EExxaammppllee :: SSqquuaarree ooff aa NNuummbbeerr
  • 20. MMaatthh..hh ##iinncclluuddee << mmaatthh..hh >> ddoouubbllee ssqqrrtt (( ddoouubbllee ));; lloogg1100 ,, ppooww (( xxyy )) ,, ssiinn ,, ccooss ,, ttaann ……
  • 21. CCaallll bbyy RReeffeerreennccee  AA ffuunnccttiioonn iinn wwhhiicchh oorriiggiinnaall vvaalluuee ooff tthhee vvaarriiaabbllee iiss cchhaannggeedd  TToo ccaallll bbyy rreeffeerreennccee wwee ccaannnnoott ppaassss vvaalluuee,, wwee hhaavvee ttoo ppaassss mmeemmoorryy aaddddrreessss ooff vvaarriiaabbllee  ““&&”” iiss uusseedd ttoo ttaakkee tthhee aaddddrreessss ooff aa vvaarriiaabbllee
  • 22. EExxaammppllee:: CCaallll bbyy RReeffeerreennccee mmaaiinn (( )) {{ ddoouubbllee xx == 112233..445566 ;; ssqquuaarree (( &&xx )) ;; }} VVaalluuee ooff ‘‘xx’’ iiss nnoott ppaasssseedd ,, bbuutt tthhee mmeemmoorryy aaddddrreessss ooff ‘‘xx’’ iiss ppaasssseedd
  • 23. EExxaammppllee:: CCaallll bbyy RReeffeerreennccee x is a pointer to a variable double ssqquuaarree (( ddoouubbllee **xx )) {{ **xx == **xx ** **xx ;; }}
  • 24. PPooiinntteerrss  Pointers aarree uusseedd ttoo ppaassss aaddddrreessss ooff vvaarriiaabbllee ffoorr rreeffeerreennccee  WWee uussee ““ &&xx ”” ttoo sseenndd tthhee aaddddrreessss ooff ““ xx ““  TToo rreecceeiivvee tthhee aaddddrreessss wwee uussee ““ **xx ”” ((wwhhaatteevveerr ““ xx ”” ppooiinnttss ttoo))
  • 25. RReeccuurrssiivvee FFuunnccttiioonnss  Special function wwhhiicchh ccaann ccaallll iittsseellff xx1100 == xx ** xx99 xx99 == xx ** xx88 xx88 == xx ** xx77 …… …… xxnn == xx ** xxnn--11
  • 26. RReeccuurrssiivvee FFuunnccttiioonnss:: FFaaccttoorriiaall nn!! == nn ** ((nn--11)) ** ((nn--22)) ………….... 33 ** 22 ** 11 55!! == 55 ** 44 ** 33 ** 22 ** 11 44!! == 44 ** 33 ** 22 ** 11 55!! == 55 ** 44!! 00!! == 11
  • 27. RReeccuurrssiivvee FFuunnccttiioonnss:: FFaaccttoorriiaall lloonngg ffaaccttoorriiaall (( lloonngg nn )) {{ iiff ((nn ==== 11 )) rreettuurrnn (( nn )) ;; eellssee rreettuurrnn (( nn ** ffaaccttoorriiaall ((nn--11)) )) ;; }}
  • 28. EExxeerrcciissee TTrryy ttoo wwrriittee pprrooggrraamm ffoorr  FFiibboonnaaccccii sseerriieess  FFiinndd ‘‘ppoowweerr ooff nnuummbbeerr’’ uussiinngg rreeccuurrssiivvee tteecchhnniiqquuee
  • 29. EExxaammppllee TThhee FFiibboonnaaccccii SSeerriieess  SSeett ooff rreeccuurrssiivvee ccaallllss ttoo ffuunnccttiioonn ffiibboonnaaccccii f( 3 ) return + f( 2 ) f( 1 ) return + f( 1 ) f( 0 ) return 1 return 1 return 0
  • 30. MMaannaaggeemmeenntt IIssssuueess ooff CCoommppuutteerr TThheerree aarree ttwwoo iissssuueess iinnssiiddee aa ccoommppuutteerr  MMeemmoorryy oovveerrhheeaadd  SSttaacckk oovveerrhheeaadd
  • 31. PPrrooggrraammmmiinngg OOppttiioonnss  EElleeggaanntt ccooddee wwhheerree pprriiccee iiss nnoott ttoooo hhiigghh  EEffffiicciieenntt ccooddee wwhheerree pprriiccee iiss ttoooo hhiigghh
  • 32. WWhhaatt hhaavvee wwee DDoonnee TTooddaayy ……  HHeeaaddeerr FFiilleess – NNiiccee mmeecchhaanniissmm ooff ppuuttttiinngg aallll pprroottoottyyppeess aanndd ddeeffiinniittiioonnss ooff gglloobbaall ccoonnssttaannttss eettcc..  SSccooppee ooff vvaarriiaabblleess  FFuunnccttiioonnss – CCaallll bbyy vvaalluuee – CCaallll bbyy rreeffeerreennccee  RReeccuurrssiioonn