unit 4 pps
unit 4 pps
9f(
Reusabiiycade 3 Advanrtagesi # The
One
compiler pasterecuting
called unstead
f
ggi"3 Sengthy.be tunction fun () fun();unt
asye Can to
Function is
Sepavat
functione fun ()
uncibon.ig calledpestfomm
be
cont vetun
Conmpiles dividel
a
back tn
compilesnd tunction . a
we ction.
en i fun
testing
coding . tojumps
he
specific
into taskFoNCTION )J
UNIT-4
the Sm
cthin easie Afte nEx t
statement of
te al
caling
thon exeuthng the in
sthtement
ealling cown blocks.and
A
than te
funcion.statementae
hat
poogramme povame
Lage
doing the tn
mutiple called hese
blockr that
tio
it
wl ý
fox tn calleot
u
ed
chin
int
( add add(i Detiniion passarmehe type
a, go1ng
todecleYahon.
unionthe name
+b, a a, inb)
int nunbeytuwo a, he to - (argument funchon_
Peclevation
b) intb) be
fcalling
ction
un
%d, ); executed calbe
ed name
numbee
- tun conteuns he ut
/
name( chion. Prototype
Ag(
uing afte Pustameks) wment
Pasaneley
ereution
anywherep
Buncion? qctual asan
statements PasHameky
iJt);
List/ L ’
et
ist); pogrthein
wil
ype Ary
) getch() SCan
printf() Pre
fC)
-define
Bunction
in+
nm)d", Pointf
Scant(" mumunt for
(num
facta= yetuunf} (i=l; faet findto
( (int
y Enter i<=n
hesder
tlesdefied fn)ufactoia
nctioname of
a )tt)
function
ypes ef
n
numben"); ar9ment
etinedtheey declaxation 4
dehi
functnoitnm number
gìven
using
add l)
defined
2) The
calling D
int
int Functisn maincint )vo)d
Categoy
function
of
Voidodd ()
Additim
Printf ( ntz add ())add
cadd
();unt tuncton.
()}addcalinghe
maun pint(e a+b,C=
retun c} C)
=atb C ()
)
wjthout unciohction,
and without
dueyfun
i
nadition funcion
arg
umen = argwnent
does
not
functn
calling ’ not
t d,c)
ealed
ctionfun
the hotcalle
The " and
:
-functin function
definition. calling declavqtionfn Teceive ny
and
cating hauing
ve
ren
valeno
valuet hoto cny
tunBony
datany data
honbut
Valueback
f0m t he
he local dato -to Casy valuey de'ned in
but it mnag Jayk. Yarame Paxarmeted Payed
the boaket ot the tunction and
proceng he called function etns he called function
AfHes tnction, fzom caling tunction the
Computed data back to the caling Actua Paameteas Actual pasamnetes al
pasum eders
Function wh aguMeat witth ne tn value
dafined in tun ction callng. qctua
X and y as
Vojd add ( int a, int'b); Z = add Cx, Y) Pametet.
unt main ()
Fozmal Poseamete omal panameSS ae pvamete
y20)
dahned jn' functim defni m.
odd (x9)) addint a,in+L) ane foml
Paets.
t Pauamete Passing Mehods
uord add (inta, intb) pocs pasing the agu mentr
caled funeHon
jnt c pasamele fom caling function to,
Pa ametey shod be
C=a+b The data typ and tha numbev
,c); Sarme actual Pa am ee*
o Paseame des deelased in the tunctm deliniti
the wumbey
shoulo be Sme tt
fwn cti om calliny.
Futio n with argumend and howing a. retwn value
Gall by vale: Whenhe volues R paamees
nt add Cinta, int b);
p'astes tom calliny feunctioy
nt main c ) he vaes ae cepied fnto
1 int x< l0 y=20 ) 2,
called tnchion
anto the called tunctin
z: adq (,y) ; mad to the al
lu
S4 ny changu
cled un tion then thu
no
chong
osiginad value n he calliny fun ction
int add (int a, ing b)
unbey addre rfereNce
tuso Thi method paes the trctons.
wAP to perfom Sepping y pasiametr etes to the called
using tuncton with call biy vae Of actual paam þoonametr
9n Hhe called the fomal
fun ction
pom'ny methad. them,.
-’functon decl onten receive the addsess by wing pointr
void swap (unta , intb); rain
Sol ’ ’ 0 functon dee)a
int man C) i Vojd suwap (int *a , int *b);
Ant xe 0 y20)
unt man()
;
Swap (x9); int 10 ) 9 * 20
ydX9))
printf (" Aftey swappry x - %d, printt l* Befor swapping
The actuad vauy d ndy Swap (4M, ty);
remainy wnchanged at the tine y 4, 9);
of functH'on call pintt(" Afkr swappiny x %a
Void swap ( in+ a intb) The value of actual paromeed vetn o)
1 inttemp Jet topjed into the memoy
docation of b
emp a; &
uqp fnction call int*b )
vojd swap (in4 a
a= b).
. int tomp;
Pmn4(e n swap function a d, b: d, 4,5); temp = a
Cal by Referene
ble fhyt s we
94 |S speeial vasia OlP Befuye swpping X= |0, y
Pointe: adde
another Vable.
AfteY s wapping =20,y 0
by Actick (*).
Pbinter is denoted Afte tu swap tunctim is clud ady
wigh
Decleration: int t a; b locatoy nam
Afte exeuhion
points a
[lovales actul valuy o
to0 tun cton the
a
changad.
LiBeSopt
4)9ryal
3)vauo 2) Unit +.
ti Storage
infom aon untI0} Claass_ >
Ka
Gabege
value’
9nitad
Regite.
Keywword Scape
sameife ’local -
top
-Sosye
AutoClan
ay ðfe
ey aut Registoy Sutial
Straye
MemoyMaun
foy Keywerdloalto
SCope
vegister
int} -stoaye -the
nt block Vauoable Vale
(ia hol&y
f("yd';);pint manc reno} Pprintf autoia)
nt conto)
’
;iKso) in.
claf (" Ghaaboge
), à, whichi
renains
a); valo4he }
t+) it
dec thin
laned
the
test()} mainjnt C)
S stercye
Extem
clay: (y.,a);
pintf voidtest() tC);eyt veid
between Scope Stoage
Snitia Stahc
hue Thexecuhom
e 3nitia
vae Ufe Key
doy'n+ Life ErtmKewayr Scope storage
program teyt
ex !- :- word value
Aen () Maunstorage
local ’ Memoy -
Vaiable Global ’ - Value
Main ditteent
;
static
’
zeHo
mo cla
of
ifie holds Meoy the (0)
the Vejd
4est ) teyt);
int veid
maun()
funcon ’
met ly
ir (""4,a);
printt
a0.atoint vasu
more C),
teyt
comeend.Value
to able
iles cals.
co
yed
rtinue
ainythe
when to
eXists
fact(Recusuive
n) = case2)
cade
Base )
)nt
unt tPoqam
maunt
Pinif( anwmbey");
f: (Ener
Scanf(.um)d", panif RecuYsÌYe
tunchon
else
in4 fac+
retun tetun 2) i4 fact fact
foc(4) Sx main () (n Cint
S) = (int Ya (
1) num n);
n n is
x ) )) ’
fact -factoi fun
(n-); ctËon ohewise
declaation 7
d
nuumbet
("d
Sconf
nun,
tint
t)
f,,
maintntc)
num); main )
int
sum
(intn);
ben");
num
Ans
wAP Pr
typeStore
betwen Sume
tupedato t of
Stryctre i mainunt (2
ctuemanestruct
stry ctuse
Stuand for Stanfpntf else (n)Antfib
datatype
ata d
di (i=o; retusn
type
- (%d", (" if
a9ay and feent
structue ente (n==
icnum
v0iable
vosiable fibln-1) 1)
fbnacc
seieAnd
no
data data ;itt um of
wseg-
detinedata -mame
-
Urion
);
+
togethe
name; temy))
) fib(n-2)
\nfom
ation j
he
ayyay
Can sing
majo
typethat esie
well (fin-)
fibln-2)othe
stbre
siml
can
1n
ncHo
Stuen neY endey /
stuct student
AmAN
'yoll mankJ
Scan f(%e", t v. 9ender),
206
A
hare i ):
cha name [20): 20 26 prntt("shudent
})- no ; 2G
int puts (v. nome);
/nt mavks28
9nitilisaton >
stuct student v;
V. name="AM AN boo k and
45 ) infsmatio a
wAR to accept pages.
V. mayks = 5O ; baok book pmce and boek
noume
infomati n
3"AmAN, 45, 5oj;
struct student V = # include <ste'oh
whes record
of the student
e WAP to yead D Nane 2) Roll no 3) Mayks. stuet book
contaiy folloing infomtt on
4) Genddet.
i cha name [20];
Sol ttineude <stdierh
tt incudeançeh int pay)
unt nain ( )
l struct studnt
chay mame [20) ; pintf( Entey erds);
int o|l no
int marks; Pintf ( &nter book nme ");
chagr gendey 9et ( . name );
pyintf ( Entey book pvice snd book pases
int manl) sCant("%4 x4", 4vpie,4 payes);
pvintf (Erty reaady pintf ( Book nanme is :
prnt En4 name "); puts l u. name;
gets (V: name),
printf (Enter marks and ollho); pintt(
pin tf ( book
WAP to read nt
Yoll umbe) name an pnntf( The stdentr dota is )
sntomatHon contan mayks
?
date ot bith o q student
{printt (" Name i student is ;
int dd puts( stU. name)}
/nd mm ; and no
int y9
shuct student
chor nme [20)}
int ollno)
struct DoB Vi
that can stere
Union :- Unibn i wes -delined date type
memey ocatton
dueent type a data at
SUme
pnntf( Enter na
Scanf(" %d 'tn:
-for( i-o;i<n; it+)
stuslent
tttt
Deelarai'sñ:
unin wnin
datatype va
nane
able name
data-type vasuabe -name)
( pnintf(nte recody"): uion stuclent
Pintf( ntey name"), char ane [2o;
seds (Sti].mame)) int maki
Scanf| 4 4 fSLi) makr ASi].ollno );
pin nf ( Ent oos");
Scan f(%4 4 y4,ASJ. vdd , 4sCiJ.ymmt sti) Um)y
Shrucue Runtime a shing
) We wnion key uore. . Reading
lywor
)We
The i e o nn yw
)
) MemoYy t layt lement oy membey i) Scanf ("%s,s);
membes.
he sepaate ii) geteh as (str);1ha shr (2], ch',
3) Each member
Pinting q shnng : ch getc hay l);
i) puts (sr)
wile( ch!=\)
ib) pin("s", st) ch slU;
putcha():
ch: getchat);
characty temited Poedefine fundions of shing :
Sting i- Stin is a aruy of
by null chosacte h) #inude <shing h
Declaatiey -’ chas sty [size]} 9 shcPy (S),S) ’ To copy siy Ss to
eg chos sty Cio]; Styo) sh)
2) st cat (S),S) To concatinate
Memoy Repreetatisn i
’ n shing we store (size -) choyactesd.
9niializaion olp HiHew ae you
a) Compile time ’ 3) stdenath-sh lenl) T fnd lensth sing.
chay st0 "Helle int n)
chay shy loJ ; chey s, C):"H;"}
str ( H' chay s,C) How au you};
n= stlenl S,);
n);
tus stings
4) Scmp (Si,S,) ’ T6 compos
1nt n
chau s, C) :}"Hi"):
1/
chay s, ( Hw yeu );
sing ?
AP to find eveye ot a
g
if(n:=o) Sol +inude <stclio h
l print("shings
equal);
3.
r wed to chany e the
touppe ( ’ Thir functiom Caye.
sting jn ppo
toupper ( "a,bed)
p ABCp pandome cs not ?
92: WAP 4o check 9)ven shnng s
hen point umbe
6) tolowey (): to louwer(" Asc) 3, AR t ead a shing and blank
nuunbe digìtr, nunben
abc d
of chas4 qcdes
spate and speeial sy mbclr prejent
anh shing withaut wy
u wAP to And Pojntes Aith mathe
preatinad tinchin?. MuHeltati
#indude <stdoh ) Additi sn
2) Divis)on
2) subacBorn
ind main )
3) Compasuson
1 chan shr C0; 4) 3nremen
s) Decement.
pintf | Enty shin, ") ;
9ets (st);
while ( strCiJ !- o)
lentt
itt)
that ha JNIT-S
9eneris polntey # include< stdb.h7
( DMA)
Vojd Po}ntes + data type 3}
be Used
DYNAMIC MEMORY ALLO CATION
void a its ype
9+ is
dad a alocatin
vauable f nypoint vasiable but we ’ Dynomie memsy at whine
to pojnt noma the menoy
declayed ike pointey data -ype alocattn9 cation
void key wad i) malo c () ’ Mem n8y alUo
contiyuow allocation
ant maun ( ) )) caec()
l vejd * pt } single block of mnoy
ant a l0, )Malo C) 94 aloc otes witth 9ayb age
nlocate memo Velue
chau A';
One
angunent
" 9+ 4akes ony
*Cint+) pt):
)
prinf ( 9ntesvolue - d, Syntax : byte.size)
pt = 4b ptr ( casttype *) mallc (
ptr = Ont*)malloe Cio +size of (in+))
pointr thst point
Null Pointes Nul pointer îs
meany + doe not poln4 to Caloc() 9} allcate mulHple bloek memosy
nothing that
Same sìze.
any vaid addeu
merny 94 initalizes alocoat memoy with zeo valu.
nt main( ) . 94 +akes t sunent, bne umbey elements
and second is sì2eeach lement.
p= NULL', ph Ccosttyre *) calloe Cn, elemontsi2)
ey pt = int *) caoc( 1o, 2)