0% found this document useful (0 votes)
13 views17 pages

Understanding Function Definitions in Programming

Uploaded by

gurdeepsingh1901
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Understanding Function Definitions in Programming

Uploaded by

gurdeepsingh1901
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Euntan

is we kuncton
Qwhat Exhain oth onr
Define the yntar of funton

task

The tunctons whih cue chated


Ltalld u-dnd bunctony

Th
tunctons which ale in -buill jn tumpiluar

wt bas a uniue name and it i AsLohle iet


Can be caled ony faat of
fron a ngrzm:

Aarametes ox argument assig to tun

vi ohtinal to netiin a vae t


tisn

to
i ahhinal,

thi callig
m

Sali rom tunctin, thek tn tyhe u

whil isg functin, thsee things ae imatent

1. Fution Declaratin:

ihe aiabl, al he tunetion mut be dleclauo

used

ad uoetjon
The functin deelaaton i alho kraun
Jtanuit
funtin jnatue
Page No:

pats,

) funtin Tyhe lresm hyhe)


)unction name
3) Paranmetee hit
4 Terminctin Aemicalan

Syntx
Kactan type> Function none (Aagumuatl Agamat 2Aas

Gamak Int Sumlint, int;

<win this exomha, functinetisn tybe is


i int

andbothae intgu

Eonctin Definatisn is alia caled feuncthn imdoonte

Aonme
wFunctbn heads functin delbatin
but Jith panm

Furctin Bady
bunclin!

3Funution (all

Functon s invakced from main untin ox othes

Punction that u k
ftion call
Note nton should be declered s defined befpre
Page No:

calliy in
in cproramny
Eunction Con he called by binbly usng a
uncbi
nome tolleuel by a Cochue
enclose
agumert
paznthesij

Syntex or general structue of tuntion

Kctan tyhez Funtin name lAgument Argunt,

statementi
tatemeat
statement3;

Examnhle Acdhin tosnumnbet Fucton

tinclude cstdio h
int Sumtint,Lnt) AAntin
void mainl) Declaeten

int a,b, Cns


toindf" Ente the alue f a and s

ans Sunlab); unctin


þaintf The Surs of too noiazd ani

int Sun int X Lat 4) functin Definátin

int ult;
hesult Xt}i
Page No:.

Louthud

ot a aud
Estu th value

15
The Sum af two no

Alantye of function

)Reusabilty
Can be seduce
2The length of sourte
oud the ault
)4S to locate solate

funtion.

kacilitate tp dounmodulau poraysanmy

caleulate Square of

Exomhle x)i untion Decloe

yoid munl)

bantf|nfte sáme no

lPunction callig
n2 Sguee(m);
2 suase (Aleat x)
tloat

foat t
Page No:

outhut
EnterAorme no uere 5

quise a the gin no 5 s 5,25

functin

Ardefinad
2) we de fined buncldn

DPadefiad These unctin are


aheady dafhd
Syatm branes-

The
Cocde
hgamntn can d Ae thu
cAite
exithiy
eror
Code

To se funtin ser nmwt be


awale
m,n ih
sFat) funton l's availosle

athih libra

must be
pastie
Page No:.

intf)ja anailable in sth lkb.

Cakzonie of function

0funtion ot
Functon drgument anc Aetun vel.
without
3)Funtion oith
orgument and reth a value
4) Functon with

)unction wtth
argument aud no zetnalue
Lhen a
funtin haon
Acehe data

when tdoes
it does net Aetun a vauethe callng
function cloes not ecele
Ony data fooo
hu called tuntin

Dn fact there i nadata tranáfen betern


the
ealling buntin and calle funchán
vod mak
No Dnbut
funl)

No output
Page No:

inud <stdiih
Suml;
clscY
Surn)
getch)

doesn't yYoid Suosl)


Yehn
qny
ary vte

banlle).da,&b)
catb;
Asinté

2 nos

Funin sthout Grgmeat cnd n hsa a velue

callid funehi
calliy frctin arnai
mainl) funcl)

t values Que dent


bach etun c:
IPage No:.

mainl)
2ntSum()
int a,
int Surm ); b,c

SumOj

outut nter to nolo


|02_0
functon oth o
orgumentswithout ehun elue
functin
calliny bortn
called frcb.
mun
argumantae furclint iit b)
passed
funlasb

Voicl Sumljnt, at)) vod surslinta, in


int ab int
catb
Aemla,b)

Outht Qnte two no


Sum 3a
lPage No:

Function uth ormant andlwith rctum vales

Calliny funtbìn. Gnalyiis called fndti,

main) argumerct tunlinta, its)

fun lasb);

mainl)

ut Sumlintint)_
int a bC
hintt Enter to no

Ca Sumla,b);
brintf(

int Sumuata, int )


etunc

Aame
Page No:
Gomble
thee
cOAP to
numben
fird Out moins ot o

inecle <sto.h
#iude cloni h

void mx Cint a, ntb,_intc


Void mainl)

int a,byj
bántf nte three numbe );
ani(LddAa,dbl;
mala,b,c)
getch);

Void mar Cint a, inth, int )

paint£(" A
2
2 pointfBis maximm
Page No:

Gntex three numben 3


maimm
ahain actual
argammntqnd foxmal argument
txomle

Aguntnt pased the fnetion duung


Lcalliny a called fnetin
actabl argament() 8y
bara kdetta

Argurtent Aeied in the defination of


nction calledformal argument or
paramet ers

#include <stdiah
I/ functbn Dec onati
void main( )
nt az5, ba3, any
any may (ayb): aand b are actial
bintfl"mex=2. any);
orgumata
int may int xy Int y x and y are formal

Aetesn
Page No:

alain by
Lcall
Call
vale (hau by vale) and

The barameteuy conbe baned in


tao ways dung
funition calig
) Call by value
2) call lyteene
Call by value

In call by vale, the aluts ot


of actual
haromete Copied to thu
formal paramet ea

So the
oniginal values wf the aiakleu of cally
functin Aeonein unckangd

untion ties to chonge he alu


paangeten, hasechangu llact4
haxamet u, not in actual

bxamplu Soah too numbe call aj nale

acludestdioh2
#clude <loni h
Void Cint, intl
dwap
Void nainl)

int
gr
Page No:

bintfl"bntea thealue
Sanf ("d ey)jtXandy
Soab (x, 4); lactual arametei

brintfn alees iasiele the main funtion "


tteal;

Woid Juoap (inti, int y) lltormal hoamete


int temi

intf("n alues iis)da the soah nton "

Entu value e andy3


Values inide the doa fumctin Xe [Link]

Values inide the mmin


main fanctn xz3.-5
Page No:.

Call by Refeuote

La call by x«feeo,the adaeu of the actal


basametes Cy
an aigumeat to
ealled unctn th

Canbe calliny fnctin

Call feeoe
by d led whena we oant to
phange he
alueio bcaloiables
tuntion

Swap tn numben
Lading call by efaua
aludes stdinh
Eintudmis:h
Void suat Lintt nt+);
Void maln)

Atanf(d ex,2yy
bontfnValue nsice the man
Page No:

oid dwap Cint tx, int * y)


nt temt

*y- tenmb
bint in
thevalue inaice
suoa
toitf(ln K=[Link] fnetion")
x,y);

Gnter the value t ey 3 5

value tide the mio fuaton X=s y-g


Recstie function

Recuasie unctin, a function that calls itaelf

kunctin calli stael then i knawa as


Jecusion

Recussin_
tma
we Lont to caleulate the factonial of

we
gen
Can
nuthen thn jn tmi bf Aelussisa.

hone
mlihy
Page No:.

t by n. the tn+)) IS
lanhuted

Exanble 4!= y*3


4*3*2
4*3X)+||

Terminatiny conition muat be bere cohid


Can temibdtes the Chain
Loll lead to
of
rocestyothenaie
iafiniti numben af fac

Piconal prentation of

fact (u)

reum2 actl3)

P 34att)|

tum;
yfactl)
fel;

hetun;
Page No:.

Examples find fatornial ofa girea number ig


#incudsstclio h

+incude<lonio-h
int foct Cint):
vad manl

int f,j
bnntf( Enter numbu:"

f factln);
þintfln factonalhdf)

intfact Cint n)

elue

fanx fattn-)

You might also like