e e4..
Funetions
Mntax
etuntype <tun name sCdato type ag4 dotype o
ocal uariable declaration
Functon defrei
Function call
Lretun varlable Cualue)), Function Probtue
Tese are mut
and shuuld.
There are ways to plement uetions
0 Function with argument&with retun tye
Function without apument with retuntype
Function with argunent & oitouE retuntype
Runction thout aqument 2 without retumtye
on
o e f i n i t i o n
Function
Void Sumc
int a b , sum=0
printP CEnter tuo numbers: ");
scan chd zd" la,Lb)
nbers:"sh
Sum = a t b :
iDqmoda 0
print PCum /.d\n, sum)
=
void ainc
Sum C , Funetion call
K
Sumc ,
prirttC"Hello"):
SumC F u n t i o n cal
Aduantage Lunot krA
3 eeuabil
to the
dehug the muam.
a t ' s easy
)Better Mem0y tilizatfon
utilizat+on.
A) Reduce the complekity o prugram
4Function t h grqumenl &th return typ
main c b
vord
'nt a,b, s
printt C Enter tuo inteaCrs i n
scar c d Vd la, 2b)
Szaddition Ca,b)
pintCsum oa lb = /d,
nt addition Cintr, fnty Srior o
Yetum *
0lelul
Funotion atkhout retuntype Othgument
oid mounC )
int a, b s
print enter two integers In"):
Scan Cd d la, Lb:
Ssubstraction Ca,b)
Void subsbraction Cint P intq
pintfC substraction =Yd" p-q di
Function without augument withetun Dpe
Void mainc
int a, b
Printt cmultiplication y, d multC ): s
int mutic
Pirtt Center too inteacrs for mlt In
Scon c),d hd " 22. Qy)
Yetun *
unetion without argument 6 without Tetuntype
reluntyeR
vofd main c
diufsionc
void druisson c
tnt a,b 3on
printt Ce ter tuo inteaers n") om6iou
SoanP- C /-d . d ", Ra, ib),
pirtt C division= 4P a/b)
Note
Potocol Prom examyples sooed
n t acddition Cint, int);
void substraction Cint, int
tmutic );
Joid divisionc)
ReCursion
A Function which call iteeH again and
again
based on
conditon iscalled Re curSion
condition called Base Condition.
Here the
Recursions
There
ure
2 ypeS
Ondirect Recursion.
Direct Recursion.
Tndirect Reeusion
Pun 1C Pun2C
PunRC Punic)
i r e c t Recusion 2rs
mainc
tunc )
L un c
Punc ) -
3
Advant o Recursion
O zt is one type oPTechic
usetulin the
Recursion is
vere very
Docta nctureS
isodortnges r
reeds to naintain Pollow the staok
E
DEis more comple than while & For lcops
Program ouehesd OcurS
oa
k i t e a prorm to Ad
factorial
number uSinq Recureion.
Aactoial n
SxY32xl
int mainc
i n t num S
no t o
Pind factorial: ")
pintP C Enter a
canP 7.d, 2 nu)
PintPCPactorial 7.ld" factovial Cnum))
2
PacC4)
lorg int Pactorial Cint n PacC
P-Cn ==o) 3+ PacC
reun 4
*PacuD
else
return
n Pactovial Cn-). Pacc
Recureion.
sum d numbeb
t mainc)
int numi
nunter ")
printPC t e
o
scanP C 7.d lnum)
printPC sum d-numbers 7.dSumCnum));
5+um Ca)
frt Sum Cint n
t +umC3)
6
Tetwn n
urt 3 +SumR)
else R +SumC
return ntsumCn-1)
4+SumC
o
power a number L
i n t mainc
2int b P
printPcEnter base no=)
scanf C 7.d", ab)
value =")
pintCter pouer
scanC/d ep
print CezpC bp)
pC2,4)
2
bi cpC2.3)
ong epC intx, int y
R*eplz,2)
J 4
retun 1 atepl
else 2expa
1
retwn *
ep Cz,{-
,)
*tageaasGeS
*Ex Fibonace Senes u
Aintlude estdio h>
int maínC
int , i,Y D,LI, , 3, 5, 8
printte Enter a no ")
scon-Ca", Ln) blO retun o
For Ci=o len;i4+) retum 4
2<t0 CRA) CR-1)
print PC Abd))
O+
310 Ca-) +C3-
A)R =3
int bCínt
PiLIo
Ca o) else
Tetum O retun HCa-+ho-D }
el se i C a
G-o a Number
chude <sxdio.ha
int mainc
int
printf cnter o no: ")
SCanf Cy.d D:ly
printf C ter a no: ")
Scant Czd y):
C
du-x dr
pit is diuidend)
else d u y drex)
printf Cy is dividend')
gd Cx
int gcd Cint x, inty)
int 1/1
PCr-=)
return 1
else
return gcd Cy,
3
storage clasees
d i c a t e s
tte prgram Cuarabte) Life
S Caabil tystope d
be variable
Four ypes
O utod
Etern
statíe
Register
storo Scpe pefault Memor LIfetie
elass Value Allocation
CeccesS)
OAuutwmatic Lit in Grar boge k e Bock
vavfables Block valuess RAM
Etern out oP 2ero In lkee Entire
Coloal tthe PrDg ram.
RAM
Uarablej le
static with in zerd Entire
Block RAM TOm.
Reaister Local narboge cP.U Bock.
with in value
loc
Rre Processor Directiues
t s used as text replacement tod
tpa.
l u o S start with
Tt reduces te
comdexitH Pgn
E # inelude
defire
#undef
d e
1P-ndeP
elbe
elif
#endiP
#pragma
# ev
indude irelde cstdioh-
include is used as HeoderPle,
0*
e con alse use like this Linclude First-e
Fdafire er2e
tntt arr Cstze]
dePine PRINT Pintectai elcome
PRTNT
deine AREACa) C3-14elS
Ca-1ls*aa
Void moun C
float =3.2, area,
area = AREAL)
un def-
#wnd et NUL
dePine NULL O
i deP
H iP deP NoLL True
#undef NULL
deline NULL O
endiP les
Hetit
For multiple conditions
Pro9Mo
Directiue used
t i a special Purpose
b
on or cP Some featuras
tw
t urn