+ All Categories
Transcript

Ph

oto

nic

Mo

de

ling

an

d D

es

ign

La

b.

Gra

du

ate

Ins

titute

of P

ho

ton

ics

an

d O

pto

ele

ctro

nic

s &

De

pa

rtme

nt o

f Ele

ctric

al E

ng

ine

erin

g

Na

tion

al T

aiw

an

Un

ive

rsity

C++C++C++C++Programming

ProgrammingProgrammingProgramming

Chapter Chapter Chapter Chapter 5 Functions and an

5 Functions and an 5 Functions and an 5 Functions and an

Introduction to RecursionIntroduction to RecursionIntroduction to RecursionIntroduction to Recursion

YihYihYih Yih--- -PengPengPengPengChiou

ChiouChiouChiou

Room 617, BL BuildingRoom 617, BL BuildingRoom 617, BL BuildingRoom 617, BL Building

(02) 3366(02) 3366(02) 3366(02) 3366--- -3603

360336033603

[email protected]

[email protected]

[email protected]

[email protected]

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

2

ObjectivesObjectivesObjectivesObjectives

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

3

OutlineOutlineOutlineOutline

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

4

OutlineOutlineOutlineOutline

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

on

struct p

rog

rams fro

m sm

all, simp

le pieces, o

r com

po

nen

ts.

�d

ivid

e and

con

qu

er

�D

eclare and

use fu

nctio

ns to

facilitate the d

esign

, imp

lemen

tation

, o

peratio

n an

d m

ainten

ance o

f large p

rog

rams

�O

verv

iew sev

eral C+

+ S

tand

ard L

ibrary

math

fun

ction

s

�L

earn h

ow

to d

eclare yo

ur o

wn

fun

ction

s

�R

and

om

nu

mb

er gen

eration

and

a versio

n o

f the casin

o d

ice gam

e

�sto

rage classes an

d sco

pe ru

les

�im

pro

ve p

rog

ram p

erform

ance

�in

line fu

nctio

ns an

d referen

ce param

eters

�F

un

ction

ov

erload

ing

-o

ne fu

nctio

n o

f the sam

e nam

e

�p

erform

similar task

s for arg

um

ents o

f differen

t typ

es or p

ossib

ly

for d

ifferent n

um

bers o

f argu

men

ts

�F

un

ction

temp

lates -a m

echan

ism fo

r defin

ing

a family

of o

verlo

aded

fu

nctio

ns

�R

ecursio

n -

fun

ction

s that call th

emselv

es

5

5.1Introduction

IntroductionIntroductionIntroduction

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

++

pro

gram

s are typ

ically w

ritten b

y co

mb

inin

g

�n

ew fu

nctio

ns an

d classes y

ou

write w

ith “p

repack

aged

” fun

ction

s

�classes av

ailable in

the C

++

Stan

dard

Lib

rary

�C

++

Stan

dard

Lib

rary p

rov

ides

�co

mm

on

math

ematical calcu

lation

s

�strin

g m

anip

ulatio

ns

character m

anip

ulatio

ns

�in

pu

t/ou

tpu

terro

r check

ing

�m

any

oth

er usefu

l op

eration

s

�F

un

ction

s

�m

od

ularize a p

rog

ram b

y sep

arating its task

s into

self-contain

ed u

nits

�b

y u

ser-

user-d

efined

fun

ctions

or p

rog

ramm

er-defin

ed fu

nctio

ns

�M

otiv

ation

s for “fu

nctio

nalizin

g” a p

rog

ram

�d

ivid

e-and-co

nq

uer m

akes p

rog

ram d

evelo

pm

ent m

ore m

anag

eable

�so

ftware reu

sability

�usin

g ex

isting fu

nctio

ns as b

uild

ing b

lock

s to create n

ew p

rogram

s

�av

oid

repeatin

g co

de in

a pro

gram

6

5.2

Pro

gra

m C

om

po

ne

nts

in C

++

��

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

fun

ction

is

inv

ok

ed b

y a

fun

ction

call, and

wh

en th

e called

fun

ction

com

pletes

its task, it eith

er

return

s a result o

r

simp

ly retu

rns

con

trol to

the

caller

7

5.2

Pro

gra

m C

om

po

ne

nts

in C

++

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�F

un

ction

s typ

es

�m

emb

ers of a class

�g

lob

al fun

ction

s -n

ot m

emb

ers of a class

�<cmath>

head

er file -

perfo

rm co

mm

on

math

ematical calcu

lation

s

�G

lob

al fun

ctions (all) –

type fx_name(arguments)�

Arg

um

ents: co

nstan

ts, variab

les or m

ore co

mp

lex ex

pressio

ns

8

5.3

Ma

th L

ibra

ry F

un

ctio

ns

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�http://msdn.microsoft.com/en-us/library/7wsh95e5.aspx

9

5.3

Ma

th L

ibra

ry F

un

ctio

ns

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

10

5.4

Fu

nc

tion

De

finitio

ns

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�fo

rmat o

f a fun

ction

defin

ition

retu

rn-v

alu

e-ty

pe

functio

n-n

am

e(para

mete

r-list)

{decla

ratio

ns a

nd sta

tem

ents

}

�retu

rn-va

lue-typ

evoid -

a fun

ction

do

es no

t return

a valu

e�

All v

ariables d

efined

in fu

nctio

n d

efinitio

ns are lo

cal variab

les—th

ey’re k

no

wn

on

ly in

the fu

nctio

n in

wh

ich th

ey’re d

efined

�A

fun

ction

’s param

eters are also lo

cal variab

les of th

at fun

ction

.�

Fu

nctio

n square

(lines 1

9–

22

) receives a co

py

of th

e valu

e of arg

um

ent x

from

line 1

3 an

d sto

res it in th

e param

eter y.

�L

ine 6

is a fun

ction

pro

toty

pe.

�N

ot req

uired

if the d

efinitio

n ap

pears b

efo

re th

e functio

n’s first u

se in th

e

pro

gram

-th

e functio

n h

eader also

serves as th

e functio

n p

roto

type

11

5.4

Fu

nc

tion

De

finitio

ns

expects to

receive an

integ

er valu

e from

the caller

info

rms th

e com

piler to

return

s an in

teger resu

lt to th

e callerC

heck

by

com

piler

If data ty

pe n

ot m

atch =

> co

nvert

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�p

ara

meter-list : a

com

ma

-sepa

rated

list con

tainin

g th

e declaratio

ns

of th

e param

eters received

by

the fu

nctio

n w

hen

it’s called

�If n

o v

alue receiv

ed, p

ara

meter-list is v

oid

or sim

ply

left emp

ty

�A

typ

e mu

st be listed

for each

param

eter in th

e param

eter list

�d

eclara

tion

s an

d sta

temen

ts : fun

ction

bo

dy

, also called

a blo

ck

or co

mp

ou

nd

statemen

t.

�V

ariables can

be d

eclared in

any b

lock

, and

blo

cks can

be n

ested.

�T

hree w

ays to

return

con

trol as a fu

nctio

n w

as inv

ok

ed

�n

ot retu

rnin

g a resu

lt: con

trol retu

rns w

hen

the p

rog

ram reach

es the

fun

ction

-end

ing

righ

t brace, o

r by ex

ecutin

g th

e statemen

treturn;

�retu

rnin

g a resu

lt, the statem

ent

return

expre

ssion;

evalu

ates exp

ressio

nan

d retu

rns th

e valu

e of ex

pressio

n to

the

caller.

12

5.4

Fu

nc

tion

De

finitio

ns

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

13

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

14

5.5

Fu

nc

tion

s

with

Mu

ltiple

P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

15

5.5

Fu

nc

tion

s w

ith M

ultip

le P

ara

me

ters

�W

hen

maximum

is called (lin

e 20

), the p

arameters x

(y,z) is in

itialized

with

the v

alue o

f the arg

um

ent n

um

ber1

(nu

mb

er2, n

um

ber3

)

�T

here m

ust b

e on

e argu

men

t in th

e fun

ction

call for each

param

eter (also

called a fo

rmal p

arameter) in

the fu

nctio

n d

efinitio

n

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

16

5.5

Fu

nc

tion

s w

ith M

ultip

le P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

17

5.5

Fu

nc

tion

s w

ith M

ultip

le P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

fun

ction

pro

toty

pe (also

called a fu

nctio

n d

eclaration

) tells the

com

piler th

e nam

e of a fu

nctio

n, th

e typ

e of d

ata return

ed b

y th

e

fun

ction

, the n

um

ber o

f param

eters the fu

nctio

n ex

pects to

receive,

the ty

pes o

f tho

se param

eters and

the o

rder in

wh

ich th

e param

eters

of th

ose ty

pes are ex

pected

.

�C

om

piler ch

ecks calls to

a fun

ction

with

the fu

nctio

n p

roto

typ

e

�n

um

ber an

d ty

pes o

f argu

men

ts, argu

men

ts in th

e correct o

rder

�v

alue retu

rned

by

the fu

nctio

n can

be u

sed co

rrectly

18

5.6

Fu

nc

tion

Pro

toty

pe

s a

nd

Arg

um

en

t Co

erc

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

he p

ortio

n o

f a fun

ction

pro

toty

pe th

at inclu

des th

e nam

e of th

e

fun

ction

and

the ty

pes o

f its argu

men

ts (no

t inclu

din

g th

e return

typ

e)

is called th

e fun

ction

sign

ature

or sim

ply

the sig

natu

re

�F

un

ction

s in th

e sam

e scop

e mu

st ha

ve u

niq

ue

sign

atures.

�T

he sco

pe is w

here th

e fun

ction is k

no

wn

and

accessible.

19

5.6

Fu

nc

tion

Pro

toty

pe

s a

nd

Arg

um

en

t Co

erc

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

rgu

men

t coercio

n-

forcin

g arg

um

ents to

the ap

pro

priate ty

pes

specified

by

the p

arameter d

eclaration

s

�e.g

. intin

call bu

t double in p

roto

typ

e

�C

on

versio

n b

y p

rom

otio

n ru

les -If arg

um

ent v

alues th

at do

no

t co

rrespond

precisely

to th

e param

eter typ

es in th

e fun

ction p

roto

typ

e can b

e co

nv

erted b

y th

e com

piler to

the p

rop

er typ

e befo

re the fu

nctio

n is called

.

�w

itho

ut lo

sing

data, e.g

., inttodouble

�T

he ty

pe o

f each v

alue in

a mix

ed-ty

pe ex

pressio

n is p

rom

oted

to th

e “h

igh

est” typ

e in th

e exp

ression (actu

ally a tem

po

rary v

ersion

of each

valu

e is created

and

used

for th

e exp

ression

—th

e orig

inal v

alues rem

ain

un

chan

ged

).

20

5.6

Fu

nc

tion

Pro

toty

pe

s a

nd

Arg

um

en

t Co

erc

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

21

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

he C

++

Stan

dard

Lib

rary is d

ivid

ed in

to m

any

po

rtion

s, each w

ith

its ow

n h

eader file.

�T

he h

eader files co

ntain

the fu

nctio

n p

roto

typ

es for th

e related

fun

ction

s that fo

rm each

po

rtion

of th

e library

.

�T

he h

eader files also

con

tain d

efinitio

ns o

f vario

us class ty

pes an

d

fun

ction

s, as well as co

nstan

ts need

ed b

y th

ose fu

nctio

ns.

�A

head

er file “instru

cts” the co

mp

iler on

ho

w to

interface w

ith

library

and

user-w

ritten co

mp

on

ents.

22

5.7

C+

+ S

tan

da

rd L

ibra

ry H

ea

de

r File

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

23

5.7

C+

+ S

tan

da

rd L

ibra

ry H

ea

de

r File

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

24

5.7

C+

+ S

tan

da

rd L

ibra

ry H

ea

de

r File

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

25

5.7

C+

+ S

tan

da

rd L

ibra

ry H

ea

de

r File

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

26

5.7

C+

+ S

tan

da

rd L

ibra

ry H

ea

de

r File

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�D

evelo

p a g

ame-p

layin

g p

rog

ram th

at inclu

des m

ultip

le fun

ction

s

�rand

and

RAND_MAX

in th

e <cstdlib>

head

er file

i= rand();

gen

erates an u

nsig

ned

integ

er betw

een 0

and

RAND_MAX

�V

isual S

tud

io: RAND_MAX

= 3

27

67

= 2

15-1

�G

NU

C+

+: RAND_MAX

= 2

14

748

364

7 =

23

1-1

�ev

ery n

um

ber b

etween

0 an

d RAND_MAX

has an

equ

al cha

nce (o

r

pro

bab

ility) o

f bein

g ch

osen

each tim

e rand

is called

�S

caling

1 + rand() % 6

�6

: scaling

factor, sh

ift the ran

ge to

1-6

from

0-5

by ad

din

g 1

�F

un

ction

rand

actually

gen

erates pseu

do

rand

om

nu

mb

ers

�R

epeatab

ility is an

imp

ortan

t characteristic o

f rand

�R

epeated

ly callin

g rand

pro

du

ces a sequ

ence o

f nu

mb

ers that ap

pears to

be ran

do

m, b

ut th

e sequ

ence rep

eats itself each tim

e the p

rog

ram ex

ecutes.

27

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

28

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

o sh

ow

that th

e nu

mb

ers pro

duced

by rand

occu

r with

app

rox

imately

equ

al likelih

oo

d, F

ig.5

.8 sim

ulates 6

,00

0,0

00 ro

lls of a d

ie

29

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

30

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

31

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�srand-

in h

eader file <

cstdlib>

, it takes an

unsigned

integ

er

argu

men

t and

seeds

the r

and

fun

ction

to p

rod

uce a d

ifferent seq

uen

ce of

rand

om

nu

mb

ers for each

execu

tion

.

�unsignedint:

�tw

o-b

ytes: 0

–6

553

5 (2

16-1

) fou

r-bytes: 0

–4

294

967

295

(23

2-1)

32

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

33

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

o ran

do

mize w

itho

ut h

avin

g to

enter a seed

each tim

esrand( time( 0

) );

�time

in <ctime>

�ty

pically

return

s the cu

rrent tim

e as the n

um

ber o

f seconds sin

ce January

1,

1970, at m

idnig

ht G

reenw

ich M

ean T

ime (G

MT

)

�R

ollin

g a six

-sided

die w

ith th

e statemen

tface = 1+ rand() % 6;

�g

eneralize th

is result as

nu

mb

er=

shiftin

gV

alu

e+rand() %

scalin

gF

acto

r;

34

5.8

Ca

se

Stu

dy

: Ra

nd

om

Nu

mb

er G

en

era

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

raps:

On

e of th

e mo

st po

pu

lar gam

es of ch

ance p

layed

in casin

os an

d

back

alleys w

orld

wid

e.

�A

play

er rolls tw

o d

ice. Each

die h

as six faces. T

hese faces co

ntain

1, 2

, 3, 4

, 5

and 6

spots. A

fter the d

ice hav

e com

e to rest, th

e sum

of th

e spots o

n th

e two

upw

ard faces is calcu

lated. If th

e sum

is 7 o

r 11 o

n th

e first roll, th

e play

er win

s. If th

e sum

is 2, 3

or 1

2 o

n th

e first roll (called

“craps”), th

e play

er loses (i.e.,

the “h

ouse” w

ins). If th

e sum

is 4, 5

, 6, 8

, 9 o

r 10 o

n th

e first roll, th

en th

at sum

beco

mes th

e play

er’s “poin

t.” To w

in, y

ou m

ust co

ntin

ue ro

lling th

e dice u

ntil

you “m

ake y

our p

oin

t.” The p

layer lo

ses by

rollin

g a 7

befo

re mak

ing th

e poin

t.

�E

nu

mera

tion

: declares a u

ser-defin

ed ty

pe

�gameStatus

is declared

to b

e of n

ew ty

pe S

tatus

�set o

f integ

er con

stants rep

resented

by id

entifiers

�T

he v

alues o

f these en

um

eration

con

stants

start at 0, u

nless sp

ecified

oth

erwise, an

d in

cremen

t by 1

�enumMonths {

JAN

= 1, FEB, MAR, APR, MAY, JUN, JUL, AUG,

SEP, OCT, NOV, DEC };

�resu

lting in

the v

alues 1

thro

ugh

12

35

5.9

Ca

se

Stu

dy: G

am

e o

f Ch

an

ce

; Intro

du

cin

g enum

enum

enum

enum

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

36

5.9

Ca

se

Stu

dy: G

am

e o

f Ch

an

ce

; Intro

du

cin

g enum

enum

enum

enum

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

37

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

38

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

39

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

40

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

he attrib

utes o

f variab

les inclu

de n

ame, ty

pe, size an

d v

alue

�E

ach id

entifier in

a pro

gram

has o

ther attrib

utes, in

clud

ing

storag

e class,

scop

ean

d lin

kag

e

�C

++

pro

vid

es five sto

rage-class sp

ecifiers: auto

, register

,

extern

, mutable

and

static

�A

n id

entifier’s sto

rag

e class d

etermin

esth

e perio

d d

urin

g w

hich

tha

t

iden

tifier exists in

mem

ory

.

�A

n id

entifier’s sco

pe

is wh

ere the id

entifier can

be referen

ced in

a

pro

gram

�th

roug

hou

t a pro

gram

; oth

ers from

limited

po

rtions o

f a pro

gram

. (5.1

1)

�A

n id

entifier’s lin

ka

ge

determ

ines w

heth

er it’s kn

ow

n

�o

nly

in th

e sou

rce file wh

ere it’s declared

or

�acro

ss mu

ltiple files th

at are com

piled

, then

link

ed to

geth

er.

�A

n id

entifier’s sto

rage-class sp

ecifierh

elps d

etermin

e its storag

e class

and

link

age.

41

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

torag

e-classsp

ecifierscan

be sp

lit into

two

: auto

matic

and

static

�A

uto

ma

ticsto

rage class: k

eyw

ord

s auto

and

register

�v

ariables are created

wh

en p

rog

ram ex

ecutio

n en

ters the b

lock

in

wh

ich th

ey’re d

efined

�th

ey ex

ist wh

ile the b

lock

is active an

d th

ey’re d

estroy

ed w

hen

the p

rog

ram ex

its the b

lock

.

�S

tatic

storag

e class: key

wo

rds e

xtern

and

static

�v

ariables ex

ist from

the p

oin

t at wh

ich th

e pro

gram

beg

ins

execu

tion

and

last for th

e du

ration

of th

e pro

gram

�allo

cated w

hen

the p

rog

ram b

egin

s execu

tion

�in

itialized o

nce w

hen

its declaratio

n is en

cou

ntered

�fo

r fun

ction

s, the n

ame o

f the fu

nctio

n ex

ists wh

en th

e pro

gram

b

egin

s execu

tion

, just as fo

r all oth

er fun

ction

s

�n

ot n

ecessary u

sed th

rou

gh

ou

t the p

rog

ram ev

en th

ou

gh

they

ex

ist from

the start o

f pro

gram

execu

tion

42

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

uto

matic sto

rage class

�o

nly

local v

ariables o

f a fun

ction

can b

e�

E.g

. auto doublex;

exists o

nly

in th

e nearest en

closin

g p

air of cu

rly b

races with

in th

e b

od

y o

f the fu

nctio

n in

wh

ich th

e defin

ition ap

pears:

�L

ocal v

ariables are o

f auto

matic sto

rage class b

y d

efau

lt, so k

eyw

ord

auto

rarely

is used

.

43

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�register intcounter = 1;

�su

gg

eststh

at the in

teger v

ariable c

ounter

be p

laced in

on

e of th

e

com

pu

ter’s registers (in

mach

ine-lan

gu

age n

orm

ally lo

aded

into

a register)

�T

he co

mp

iler mig

ht ig

no

re register

declaratio

ns, e.g

., insu

fficient

�register

can b

e used

on

ly w

ith lo

cal variab

les and

fun

ction p

arameters.

44

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

tatic

-k

eyw

ord

s extern

and

static

declare id

entifiers fo

r variab

les

�S

tatic-storag

e-class variab

les ( and

the n

ame o

f fun

ctions) ex

ist (allo

cated) fro

m th

e po

int at w

hich

the p

rog

ram b

egin

s execu

tion

and

last fo

r the d

uratio

n o

f the p

rog

ram.

�S

uch

a variab

le is initialized

on

ce wh

en its d

eclaration

is enco

un

tered.

�T

wo

typ

es of id

entifiers

�ex

ternal id

entifiers (su

ch as g

lob

al variab

lesan

d g

lob

al fun

ction

nam

es)

�lo

cal variab

les declared

with

the sto

rage-class sp

ecifierstatic

.

�G

lob

al variab

les

�p

lacing

variab

le declaratio

ns o

utsid

e any class o

r fun

ction d

efinitio

n.

�retain

their v

alues th

rou

ghou

t the ex

ecutio

n o

f the p

rog

ram.

�G

lob

al variab

les and

glo

bal fu

nctio

ns can

be referen

ced b

y an

y fu

nctio

n

that fo

llow

s their d

eclaration

s or d

efinitio

ns in

the so

urce file

45

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�L

ocal v

ariables d

eclared static

�e.g.

static intcount = 1;

�k

no

wn

on

ly in

the fu

nctio

n in

wh

ich th

ey’re d

eclared,

�b

ut retain

their v

alues w

hen

the fu

nctio

n retu

rns to

its caller.

�T

he n

ext tim

e the fu

nctio

n is called

, the s

tatic

local v

ariables

con

tain th

e valu

es they

had

wh

en th

e fun

ction last co

mp

leted ex

ecutio

n.

�A

ll nu

meric v

ariables o

f the static sto

rage class are in

itialized

to zero

if th

ey’re n

ot ex

plicitly

initialized

by y

ou

46

5.1

0S

tora

ge

Cla

ss

es

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

cop

eo

f an id

entifier: p

ortio

n o

f the p

rog

ram w

here it ca

n b

e used

�E

.g., d

eclare a local v

ariable in

a blo

ck: it can

be referen

ced o

nly

in

that b

lock

and

in b

lock

s nested

with

in th

at blo

ck.

�T

his sectio

n—

functio

nsco

pe, g

lob

al nam

espace sco

pe, lo

cal scop

ean

d

fun

ction-p

roto

typ

e scop

e

�tw

o o

ther sco

pes—

class scop

e(C

h.9

) and

nam

espace sco

pe

(Ch

.2

4)

�g

lob

al n

am

espa

ce scop

e

�A

n id

entifier d

eclared o

utsid

e any fu

nctio

n o

r class. it is “kn

ow

n” in

all

fun

ctions fro

m th

e po

int at w

hich

it’s declared

un

til the en

d o

f the file

�G

lob

al variab

les, fun

ction d

efinitio

ns an

d fu

nctio

n p

roto

typ

es placed

ou

tside a fu

nctio

n

�L

abels

(iden

tifiers follo

wed

by a co

lon

such

as start:

) are the o

nly

iden

tifiers with

fun

ction

scop

e.

�can

be u

sed an

yw

here in

the fu

nctio

n in

which

they

appear, b

ut n

ot b

e outsid

e

�used

in goto

statemen

ts (Appen

dix

F).

�im

plem

entatio

n d

etails that fu

nctio

ns h

ide fro

m o

ne an

oth

er.

47

5.1

1S

co

pe

Ru

les

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�L

oca

lsco

pe -

iden

tifiers declared

insid

e a blo

ck

�b

egin

s at the id

entifier’s d

eclaration

and

end

s at the term

inatin

g rig

ht

brace (}

) of th

e blo

ck in

wh

ich th

e iden

tifier is declared

�L

ocal v

ariables (in

cludin

g static)

, fun

ction p

arameters

�A

ny b

lock

can co

ntain

variab

le declaratio

ns.

�W

hen

blo

cks are n

ested an

d an

iden

tifier in an

ou

ter blo

ck h

as the sam

e n

ame as an

iden

tifier in an

inn

er blo

ck, th

e iden

tifier in th

e ou

ter blo

ck is

“hid

den

” un

til the in

ner b

lock

termin

ates.

48

5.1

1S

co

pe

Ru

les

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�F

un

ction

pro

toty

pe sco

pe: o

nly

iden

tifiers in th

e param

eter list of a fu

nctio

n

pro

toty

pe (n

ames n

ot req

uired

, only

types req

uired

)

�N

ames in

the list are ig

nored

by

the co

mpiler

�Id

entifiers u

sed in

a functio

n p

roto

type can

be reu

sed elsew

here in

the p

rogram

w

ithout am

big

uity

�In

a single p

roto

type, a p

articular id

entifier can

be u

sed o

nly

once

49

5.1

1S

co

pe

Ru

les

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

50

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

51

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

tack

: a data stru

cture an

alog

ou

s to a p

ile of d

ishes

�p

ush

ing th

e dish

on

to th

e stack, an

d p

op

pin

g th

e dish

off th

e stack (b

oth

from

the to

p)

�last-in

, first-out (L

IFO

) data stru

ctures

�F

un

ction

call stack(p

rogram

execu

tion

stack)

�w

ork

ing “b

ehin

d th

e scenes” -

sup

po

rts the fu

nctio

n call/retu

rn m

echan

ism

�su

pp

orts th

e creation

, main

tenan

ce and

destru

ction o

f each called

fun

ction’s au

tom

atic variab

les

�as each

fun

ction (fx

) is called

, it may

, in tu

rn, call o

ther fx

(s)

�k

eep tra

ck o

f the retu

rn a

dd

resses to retu

rn co

ntro

l to th

e fxcallin

g it

�E

ach tim

e a fxcalls an

oth

er fx, a

n en

try is p

ush

ed o

nto

the sta

ck

�T

his en

try, called

a stack

fram

eo

r an activ

ation

record

, con

tains th

e

return

add

ress and

som

e add

ition

al info

rmatio

n

52

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�If th

e called fu

nctio

n retu

rns, in

stead o

f calling

ano

ther fu

nctio

n b

efore

return

ing, th

e stack

fram

e for th

e fun

ction

call is p

op

ped

, and

con

trol

transfers to

the retu

rn ad

dress in

the p

op

ped

stack fram

e.

�called

fun

ction

find

s the in

form

ation

to retu

rn at th

eto

po

f the stack

�If o

ne fu

nctio

n m

akes a call to

ano

ther, a n

ew stack

frame is p

ush

ed

�T

hu

s, the retu

rn ad

dress req

uired

by th

e new

ly called

fun

ction to

retu

rn to

its caller is no

w lo

cated at th

e top

of th

e stack.

�A

no

ther im

po

rtant resp

on

sibility

of stack

frame

�A

uto

ma

tic varia

bles, p

arameters an

d lo

cal variab

les the fu

nctio

n d

eclares

�n

eed to

exist w

hile a fu

nctio

n is ex

ecutin

g

�n

eed to

remain

active if th

e fun

ction m

akes calls to

oth

er fun

ctions

�W

hen

called fu

nctio

n retu

rns—

and

no

lon

ger n

eeds its lo

cal auto

matic

variab

les—its stack

frame is p

op

ped

from

the stack

, and

tho

se local

auto

matic v

ariables are n

o lo

ng

er kn

ow

n to

the p

rog

ram

�O

nly

a certain am

ou

nt o

f mem

ory

can b

e used

to sto

re activatio

n reco

rds o

n

the fu

nctio

n call stack

du

e to fin

ite com

pu

ter mem

ory

�stack

ov

erflow

-if m

ore fu

nctio

n calls o

ccur th

an can

stored

53

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

54

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�activ

ation

record

�1. OS

calls main: retu

rn ad

dress R

1 an

d au

to-v

ar. a�

2. main

calls square: retu

rn ad

dressR2 an

d au

to-v

ar. x

55

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

56

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�square retu

rns to

main: stack

is po

pp

ed, g

ives R2

and

loo

sing

x�

main return

s to O

S: stack is p

op

ped

again

, giv

es R1an

d lo

osin

g a

57

5.1

2F

un

ctio

n C

all S

tac

k a

nd

Ac

tiva

tion

Re

co

rds

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

n em

pty

param

eter list: writin

g eith

er void

or n

oth

ing at all in

paren

theses

�T

he p

roto

typ

e

voidprint();

specifies th

at fun

ction

print

do

es no

t take arg

um

ents an

d d

oes n

ot retu

rn

a valu

e

58

5.1

3F

un

ctio

ns

with

Em

pty

Pa

ram

ete

r Lis

ts

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

59

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

++

pro

vid

es inlin

e fun

ctions

to h

elp red

uce th

e execu

tion-tim

e ov

erhead

of fu

nctio

n calls—

especially

for sm

allfu

nctio

ns.

�q

ualifier i

nline

befo

re a fun

ction’s retu

rn ty

pe in

the fu

nctio

n d

efinitio

n:

“adv

ises” the co

mp

iler to g

enerate a co

py o

f the fu

nctio

n’s co

de in

place

(wh

en ap

pro

priate) to

avo

id a fu

nctio

n call.

�T

he co

mp

iler can ig

no

re the i

nline

qu

alifier and

typ

ically d

oes so

for all

bu

t the sm

allest fun

ction

s

�trad

e-off b

etween

mem

ory

and

time

60

5.1

4In

line

Fu

nc

tion

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�K

eyw

ord

const

in fu

nctio

n cube

’s param

eter list (line 9

) tells the

com

piler th

at the fu

nctio

n d

oes n

ot m

od

ify v

ariable s

ide

, ensu

ring

side

’s valu

e is no

t chan

ged

by th

e fun

ction d

urin

g th

e calculatio

n.

�N

otice th

at the co

mp

lete defin

ition

of fu

nctio

n cube

app

ears befo

re it’s u

sed in

the p

rog

ram.

�T

his is req

uired

so th

at the co

mp

iler kn

ow

s ho

w to

exp

and

a cube

fun

ction

call into

its inlin

edco

de.

�F

or th

is reason, reu

sable in

line fu

nctio

ns are ty

pically

placed

in h

eader files,

so th

at their d

efinitio

ns can

be in

clud

ed in

each so

urce file th

at uses th

em.

61

5.1

4In

line

Fu

nc

tion

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

62

5.1

4In

line

Fu

nc

tion

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

Tw

o w

ays to

pass arg

um

ents to

fun

ction

s

�p

ass-by

-va

lue

�a co

py

of th

e argu

men

t’s valu

e is mad

e and

passed

(on

the

fun

ction

call stack) to

the called

fun

ction

�ch

ang

es to th

e cop

y d

o n

ot affect th

e orig

inal

�p

reven

ts the accid

ental sid

e effects

�p

ass-by

-reference

�th

e caller giv

es the called

fun

ction

the ab

ility to

access th

e

caller’s d

ata

directly, an

d to

mo

dify

that d

ata

�referen

ce param

eter: an alias fo

r its corresp

on

din

g arg

um

ent in

a

fun

ction

call.

�fu

nctio

n p

roto

typ

e by

an am

persan

d (&

)

�th

e orig

inal v

ariable can

be m

od

ified b

y th

e called fu

nctio

n

�to

specify

a reference to

a con

stant, p

lace the c

onst

qu

alifier

�F

un

ction

s can retu

rn referen

ces, bu

t this can

be d

ang

erous.

63

5.1

5R

efe

ren

ce

s a

nd

Re

fere

nc

e P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

64

5.1

5R

efe

ren

ce

s a

nd

Re

fere

nc

e P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

65

5.1

5R

efe

ren

ce

s a

nd

Re

fere

nc

e P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

66

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�R

eferences can

also b

e used

as alia

sesfo

r oth

er variab

les

with

in a fu

nctio

n // u

se the sam

e mem

ory

67

5.1

5R

efe

ren

ce

s a

nd

Re

fere

nc

e P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

68

5.1

5R

efe

ren

ce

s a

nd

Re

fere

nc

e P

ara

me

ters

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�U

sed in

a fu

nctio

n rep

eated

ly w

ith th

e sam

e arg

um

ent v

alu

e for

a p

articu

lar p

ara

meter

�O

mits an

argu

men

t for a p

arameter w

ith a d

efault arg

um

ent in

a fu

nctio

n call, th

e com

piler rew

rites the fu

nctio

n call an

d in

serts the

defau

lt valu

e of th

at argu

men

t.

�D

efault arg

um

ents m

ust b

e the rig

htm

ost (trailin

g) arg

um

ents in

a fu

nctio

n’s p

arameter list.

�D

efault arg

um

ents m

ust b

e specified

with

the first o

ccurren

ce of th

e

fun

ction

nam

e—ty

pically

, in th

e fun

ction

pro

toty

pe.

�D

efault v

alues can

be an

y ex

pressio

n, in

clud

ing

con

stants, g

lob

al v

ariables o

r fun

ction

calls.

�D

efault arg

um

ents also

can b

e used

with

inline

fun

ction

s.

69

5.1

6D

efa

ult A

rgu

me

nts

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

70

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

71

5.1

6D

efa

ult A

rgu

me

nts

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�u

nary

scop

e resolu

tion

op

erator (:

:)

: to access a g

lob

al

variab

le wh

en a lo

cal variab

le of th

e same n

ame is in

scop

e

72

5.1

7U

na

ry S

co

pe

Re

so

lutio

n O

pe

rato

r

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

73

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�F

un

ction

s of th

e sam

e na

me w

ith d

ifferent sig

na

tures

�U

sed to

create several fu

nctio

ns o

f the sam

e nam

e that p

erform

similar

tasks, b

ut o

n d

ifferent d

ata typ

es

�T

he co

mp

iler uses o

nly

the p

arameter lists to

distin

gu

ish b

etween

ov

erload

ed fu

nctio

ns.

�F

un

ction

main

is no

t man

gled

, becau

se it cann

ot b

e ov

erload

ed.

�U

se cautio

n w

hen

ov

erload

ing

fun

ction

s with

defau

lt param

eters,

becau

se this m

ay cau

se amb

igu

ity,

�e.g

. a fxw

ith 3

par. &

a fx. w

ith 3

par +

1 d

efault p

ar. (com

pilatio

n erro

r)

74

5.1

8F

un

ctio

n O

ve

rloa

din

g

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

75

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�F

un

ction

temp

lates:id

entica

lp

rog

ram lo

gic an

d o

peratio

ns fo

r

�N

ote: O

verlo

aded

fun

ctions p

erform

similar o

peratio

ns th

at inv

olv

e

differen

t pro

gram

log

ic on

differen

t data ty

pes.

�D

efines a w

ho

le family

of o

verlo

aded

fun

ction

s

�G

iven

the arg

um

ent ty

pes =

> au

tom

atically g

enerates sep

arate fun

ction

temp

late specializatio

ns

to h

and

le each ty

pe o

f call app

rop

riately

76

5.1

9F

un

ctio

n T

em

pla

tes

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

77

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

78

5.1

9F

un

ctio

n T

em

pla

tes

�E

very

param

eter in th

e temp

late param

eter list (often

referred to

as a form

al typ

e param

eter) is preced

ed b

y k

eyw

ord

typename

or k

eyw

ord

class

(wh

ich are sy

no

ny

ms in

this co

ntex

t).

�T

he fo

rmal ty

pe p

arameters are p

laceho

lders fo

r fun

dam

ental

typ

es or u

ser-defin

ed ty

pes.

�T

he n

ame o

f a typ

e param

eter mu

st be u

niq

ue in

the tem

plate

param

eter list for a p

articular tem

plate d

efinitio

n.

�M

ore th

an o

ne class, e.g

. template <class T1, class T2>

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�R

ecursiv

e fun

ction: fu

nctio

n callin

g itself, eith

er directly

, or in

directly

(thro

ug

h an

oth

er fun

ction)

�B

ase case:fu

nctio

n so

lvin

g o

nly

the sim

plest case, sim

ply

return

s a result

�m

ore co

mp

lex: d

ivid

ing th

e pro

blem

into

two

con

ceptu

al pieces

�A

: a piece th

at the fu

nctio

n k

no

ws h

ow

to d

o an

d

�B

: a piece th

at it do

es no

t kn

ow

ho

w to

do

�T

o m

ake recu

rsion feasib

le, B m

ust resem

ble th

e orig

inal p

rob

lem, b

ut

be a slig

htly

simp

ler or sm

aller versio

n.

�T

his n

ew p

rob

lem lo

ok

s like th

e orig

inal, so

the fu

nctio

n calls a co

py o

f itself to

wo

rk o

n th

e smaller p

rob

lem—

this is referred

to as a recu

rsive call

and

is also

called th

e recursio

n step

.

�T

he recu

rsion

step o

ften in

clud

es the k

eyw

ord

return -

its result w

ill be

com

bin

ed w

ith th

e po

rtion o

f the p

rob

lem th

e fun

ction k

new

ho

w to

solv

e to

form

the resu

lt passed

back

to th

e orig

inal caller, p

ossib

ly main

�E

ach tim

e the fu

nctio

n calls itself w

ith a slig

htly

simp

ler versio

n o

f the

orig

inal p

rob

lem in

ord

er to ev

entu

ally co

nv

erge o

n th

e base case.

�A

t that p

oin

t, the fu

nctio

n reco

gn

izes the b

ase case and

return

s a result to

the

prev

ious co

py o

f the fu

nctio

n, an

d a seq

uen

ce of retu

rns en

sues u

p th

e line

un

til the o

rigin

al call even

tually

return

s the fin

al result to

the o

rigin

al caller

79

5.2

0R

ec

urs

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�Ex. factorial

80

5.2

0R

ec

urs

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

81

5.2

0R

ec

urs

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�unsignedlongint:

>=

4 B

ytes: 0

to >

= (2

32-1

) = 4

29

49

67

29

5

�int:

4 B

ytes =

> -

23

1to

(2

31-1

) = 2

14

74

83

647

82

5.2

0R

ec

urs

ion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�F

ibo

nacci n

um

ber is th

e sum

of th

e prev

iou

s two

Fib

on

acci nu

mb

ers.

�fibonacci(0) = 0

fibonacci(1) = 1

fibonacci(n) = fibonacci(n–1) + fibonacci(n–2)

�T

he F

ibo

nacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, …

�A

series occu

rs in n

ature an

d, in

particu

lar, describ

es a form

of sp

iral.

�T

he ratio

of su

ccessive F

ibo

nacci n

um

bers co

nv

erges o

n a co

nstan

t v

alue o

f 1.6

18

….

�T

his n

um

ber, to

o, freq

uen

tly o

ccurs in

natu

re and

has b

een called

the

go

lden

ratioo

r the g

old

en m

ean.

�H

um

ans ten

d to

find

the g

old

en m

ean aesth

etically p

leasing

.

�A

rchitects o

ften d

esign

win

do

ws, ro

om

s and

bu

ildin

gs w

ho

se leng

th

and

wid

th are in

the ratio

of th

e go

lden

mean

.

�P

ostcard

s are often

desig

ned

with

a go

lden

mean

leng

th/w

idth

ratio.

83

5.2

1E

xa

mp

le U

sin

g R

ec

urs

ion

: Fib

on

ac

ci S

erie

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

84

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

85

5.2

1E

xa

mp

le U

sin

g R

ec

urs

ion

: Fib

on

ac

ci S

erie

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

++

do

es no

t specify

the o

rder in

wh

ich th

e op

erand

s of m

ost

op

erators (in

clud

ing

+) are to

be ev

aluated

.

�D

on

’t assum

e the o

rder in

wh

ich th

ese calls execu

te (It’s OK

in

Fig

.5.2

9.)

�fibonacci(n) = fibonacci(n–1) + fibonacci(n–2)

�In

som

e pro

gram

s the ev

aluatio

n o

f an o

peran

d can

hav

e side

effects(ch

ang

es to d

ata valu

es) that co

uld

affect the fin

al result o

f

the ex

pressio

n

�C

++

specifies th

e ord

er of ev

aluatio

n o

f the o

peran

ds o

f on

ly fo

ur

op

erators—

&&

, ||

, com

ma (,

) and

?:

.

86

5.2

1E

xa

mp

le U

sin

g R

ec

urs

ion

: Fib

on

ac

ci S

erie

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�E

ach lev

el of recu

rsion

in fu

nctio

n fibonacci

has a d

ou

blin

g

effect on

the n

um

ber o

f fun

ction

calls; i.e., the n

um

ber o

f recursiv

e

calls that are req

uired

to calcu

late the n

th F

ibo

nacci n

um

ber is o

n

the o

rder o

f 2n.

�C

alculatin

g th

e 20

th F

ibo

nacci n

um

ber: 2

20≈

a millio

n calls

the 3

0th

Fib

on

acci nu

mb

er: 23

0≈a b

illion

calls

�C

om

pu

ter scientists refer to

this as ex

po

nen

tial com

plex

ity.

87

5.2

1E

xa

mp

le U

sin

g R

ec

urs

ion

: Fib

on

ac

ci S

erie

s

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�B

oth

are based

on

a con

trol statem

ent:

iteration

: repetitio

nstru

cture recu

rsion

: a selection

structu

re

�B

oth

inv

olv

e repetitio

n:

exp

licitly a rep

etition

structu

re thro

ug

h rep

eated fu

nctio

n calls

�B

oth

inv

olv

e a termin

ation

test:

loo

p-co

ntin

uatio

n co

nd

ition

fails a base case is reco

gn

ized

�B

oth

grad

ually

app

roach

termin

ation

:

mo

difies a co

un

ter p

rod

uces sim

pler v

ersion

s

�B

oth

can o

ccur in

finitely

:

the lo

op

-con

tinu

ation

test nev

er beco

mes false

the recu

rsion

step d

oes n

ot co

nv

erges o

n th

e base case

88

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

89

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

90

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�N

egativ

eso

f Recu

rsion

�rep

eatedly

inv

ok

es the m

echan

ism, an

d co

nseq

uen

tly th

e o

verh

ead, o

f fun

ction

calls.

�ex

pen

sive in

bo

th p

rocesso

r time an

d m

emo

ry sp

ace.

�E

ach recu

rsive call cau

ses ano

ther co

py

of th

e fun

ction

(actually

o

nly

the fu

nctio

n’s v

ariables) to

be created

; this can

con

sum

e co

nsid

erable m

emo

ry.

�Iteratio

n n

orm

ally o

ccurs w

ithin

a fun

ction

, so th

e ov

erhead

of

repeated

fun

ction

calls and

extra m

emo

ry assig

nm

ent is o

mitted

.

91

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

92

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

93

5.2

2R

ec

urs

ion

vs

. Itera

tion

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

94

5.2

2R

ec

urs

ion

vs

. Itera

tion


Top Related