P. 1
C__ - Sinhala Copy

C__ - Sinhala Copy

3.67

|Views: 3,863|Likes:
Published by GayanthaPandula
uytu
uytu

More info:

Published by: GayanthaPandula on Mar 12, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/01/2015

pdf

text

original

Functions hkq fudkjdoehs Tn wihs' kuq;a Tn th oekgu;a oel we;' Tn by; mrspsfpso
j,oS oqgq main(), C++ ys Tng yuqfjk m,uq yd m%Odk function tlhs' kuq;a function tlla wm wrA:
olajkafka flfiao@

Function tlla hkq C++ muKla fkdj fjk;a fndfyda mrs.Kl jevigyka j,o uQ,sl ls%hdldrS
tallhhs' fjk;a jpk j,ska lsh;fyd;a functions fkdue;sj C++ u.ska mrs.Kl jevigyka ,sjSula
ms,sn`oj is;sh fkdyelsh' Function tlla ;=, wvx.= jkafka mrs.Klh ,jd lsishus ls%hdjla lr.ekSu
i`oyd ,shk ,o mrs.Kl fla; reils' Function tlla u.ska lsishus mrs.Kl fla; reila i`oyd
wdjrKhla idohs' tu fla;hka u.ska isoqfjk ldrAhh wkqj Functions tflys ku ;SrKh lrkq
,efnhs' WodyrKhla f,i mrs.Kl ;srh u; lsishus fohla print lrkakdjQ mrs.Kl fla;hka wvx.=
fldg ksujk ,o Function tlla print kuska kus l, yel' Tn oqgq main keue;s function tl fj;
tu ku ,enS we;af;a th mrs.Kl jevigykl m,uqfjkau ls%hd;aul jk function tl jk fyhsks'

Functions u.ska mrs.Kl jevigyklg isoqfjk fiajhka lSmhla meyeos,sj oelsh yelshs :

41

1' tla jrla ,shk ,o mrs.Kl fla;hka kej; kej; m%fhdackhg
.ekSfus myiqlu ie,iSu'

2' mrs.Kl jevigykla fldgia j,g fnod fjkalsrSu' fus u.ska
tu jevigyk ,sjSu iy jgyd .ekSu myiq fjs'

wm oeka function tlla hkq l=ulaoehs y|qkd.ekSfus m,uq mshjr f,i th mdjspsps lr ,shk ,o ir,
mrs.Kl jevigykla l=uk wdldrhla .k`oehs jsuid n,uq' my; oelafjk jevigyk mrSCId lr
n,kak' th bosrsm;a lr we;af;a Tng function tlla ls%hdlrK wdldrh .ek ir, f,i fmkajd oSug
muKs' tksid tys we;s ish,q foa jgyd .ekSug W;aidy fkdlrkak' Functions .ek jsia;r" wm my;
jevigyk .ek idlpspd lsrSfuka wk;=rej jev`oqrg;a i,ld n,uq:

========================================================

// introToFunctions.cpp

#include

void print();

void main()
{

print();

}

void print()
{

cout << “Hi! This is my 1st

function.” << endl;

}

========================================================

wm variables .ek l:d lsrSfusoS" variable tlla define lsrSug fmr th declare l, hq;= nj Tn oqgqjd'
^fus ls%hdj ms,sn`oj Tnf.a u;lh .s,syS we;akus lreKdlr m,uq mrspsfpsofhaoS variables hgf;a tk
fldgi iy 2 jk mrspsfpsofhaoS int – data type hgf;a tk fldgi kej; lshjkak& fuh functions
i`oydo fmdoqjk lreKls' fusksid variable tlla fukau function tlla o declare lr bkamiq define
l, hq;=hs' Function tlla m,uqj declare lsrSfuka isoqjkafka wm jevigyk ;=, tu function tl
mdjspsps lsrSug hk nj mjiuska compiler uDoqldx.hg th y`oqkajdoSuh' tjsg th jevigyk compile
lrf.k hdfusoS" function declaration tl yuqjQ jsg function tl iu. lghq;= lsrSug oek isgskq we;'
;jo" variable tlla iusnkaOfhka Tn oqgq mrsosu function tlloSo declare iy define lsrSu tlajr
isoql, yelsh'

by; jevigyfkaoS void print() hkafkka isoqjkafka fuu function tl declare lsrSfus ls%hdjhs' tys
definition tl isoqjkafka main function tlg my,ska we;s"

void print()
{

cout << “Hi! This is my 1st

function.” << endl;

}

hk fldgisks' tkus" print function tl u.ska isoqlrK ldrAhh ;SrKh lr thg kshu w.hla fokafka
fuysoSh' th isoqlrK ldrAhh kus" cout << “Hi! This is my 1st

function.” << endl hk

fla;h ls%hd;aul lrjSu nj Tng fmfkkq we;'

main() ;=,o print() f,i function tflys ku i`oyka jS we;s nj Tng oelsh yel' fuh function
call f,i ye`oskafjs' thska isoqjkafka print function tl ls%hd;aul lrjSuhs' fujsg print ;=, we;s
mrs.Kl fla;hka ls%hd;aul jSu isoqfjs' fuh isoqjk wdldrh kerTSu i`oyd Tn" by; jevigyk
debug lsrSu wdrusNlr" Visual C++ ys step in iy step over hk ls%hdldrS;ajhka u.ska jevigyk

42

ls%hd;aul jk wkqms,sfj, mrSCId l, hq;=h' ^fmdf;ys “Debug lsrSu iy Errors y|qkd.ekSuhk
fldgi lshjSfuka fuh isoq lrK wdldrh Tng W.; yelsh&' tjsg main ;=,ska wdrusNjk jevigyk
ls%hd;aul jSfus ,CIh main ;=, we;s print ys function call tl fj; meusK t;ekska print ;=,g
we;=,ajk nj Tn olskq we;' jevigyk ls%hd;aul jSfus ,CIh print ;=, we;s fla;h u;ska .uka
lsrSfuka miq th kej; main ;=,g wdmiq we;=,a fjkq we;' fujsg Terminal window tl foi Tn
ne,qjfyd;a" th ;=, “Hi! This is my 1st

function.” hkak print jS ;sfnkq Tn olskq we;' fus

wkqj by; fmfkk mrsos" Tn jsiska idok ,o functions j,g we;=,a jSfus m%Odk fodrgqj jkafka main
function tlhs'

by;oS" function tlla mrs.Kl jevigykla ;=, uQ,sl f,i ls%hd;aul jkafka flfiaoehs oelajqKd'
oeka" functions hkq fudkjdoehs jvd;a ,xj n,uq'

by; Tn oqgq mrsos function tll Ndjs;fha wjia:d 3 la we;'

1. Function declaration
2. Function definition
3. Function call
– fuu.ska isoqjkafka jevigykl lsishus ia:dkhl we;s function tlla" tu
jevigyfkys ;j;a ia:dkhl isg ^by; jevigyfkys oS kus main ys isg& ls%hd;aul
lrjSuhs'

Function tlla fldgia 3 lska iukajs;h' my; oelafjkafka tu fldgia 3 hs'

1.

Return type.

2.

Function name'

3.

Parameter list.

Function tlla le,alHQf,agrA hka;%hlg iudk l, yel' Tn le,alHQf,agrhlg o;a; we;=,a l, jsg
(input) th Tn we;=,a l, o;a;hka fjkialus j,g Ndckh lr" wdmiq Tng o;a;hla ,nd fohs
(output). Function tlla u.ska isoqjkafkao fuhhs' th msg;ska o;a; ,ndf.k" tu o;a; fjkialus
j,g Ndckh lr" tu.ska m%;s:,hla ksmojd" wdmiq o;a;hka msg lrhs' by; fldgia ;=fkka m,uq
jekak jk Return type hkafkka" Function tflka msglrK fuu m%:sm, o;a;fha o;a; jrA.h olajhs'
fojeks fldgiska olajkafka function tflys ku nj Tng meyeos,shs' f;jeks fldgi jk Parameter
list hkafkka oelafjkafka function tl" input tl f,i th ;=,g ,nd .kakd o;a;hkah' kuq;a
Parameter list tlla fyda Return type tlla fkdue;s fyj;a" input tlla fyda output tlla fkdue;s
functions o iEosh yelshs' fus jrA.fha functions u.ska lsisu o;a;hla ;ud ;=,g ,nd fkd.kakd w;r
lsisu m%:sm,hla msg;g ,nd fkdfoa' wm by;ska i,ld ne,Q wjika jevigyfkys print function tl
fujekakls' th isoql, tlu foh jQfha jpk fma,shla console window tl u; print lsrSu muKs'

1. Function declaration

Function declaration tlla u.ska isoqjkafka kj function tlla compiler uDoqldx.hg y`oqkajdoSu nj Tn
by; ia:dkhloS oqgqjd' tfiakus oeka wm by; Return type, Function name, Parameter list hk fldgia
;=fkka u iukajs; jQ function declaration tlla ,shkafka flfia oehs n,uq' my; oelafjkafka tjeks
function declaration tllg WodyrKhls:

========================================================

int add(int num1, int num2);

========================================================

43

my; oelafjkafka by; add function tflys declaration tl" by;ska Tn oqgq fldgia j,g fnod we;s
wdldrhhs'

int add(int num1, int num2)

Return type Function name

Parameter list

fuu function tl output tl f,i ,nd fok o;a;fhys jrA.h int h' fuh Return type hkafkka
oelafjs' tfukau th num1 yd num2 hk int variables hq., input tl f,i ,nd .kshs' fuh parameter
list hkafkka oelafjs' Parameter list f,i wm fu;e; oS i`oyka lr we;af;a variables 2 la muKla
kuq;a Tng leu;s TskEu variables .Kklska hq;a parameter lists iys; functions iEosh yelshs'

2. Function definition

by; function declaration tlg wod, function definition tl my; oelafjs:

===========================================================

int add(int num1, int num2)

// function head

{

//
int addition = num1 + num2; // function body
return addition;

//

}

===========================================================

add ys function declaration tlska add ys definition tl fjkia jkafka flfiao@ m,uqj thg i`.,
jryka hq.,la tl;= jS we;' i`., jryka jsjD; jk ia:dkfhka oelafjkafka add function tflys
wdrusNl ia:dkhhs' ke;fyd;a function tl n,meje;ajSug mgka.kakd ia:dkhhs' i`., jryka
jefik ia:dkfhka function tflys n, m%foaYh wjikaj hhs' Function tl ;=, wvx.= jsh hq;= ish,a,
fuu jryka w;r wka;rA.; jsh hq;=h' fuu jryka w;r fldgi function body hkqfjka ye`oskafjs'
fuys function declaration tlg iudk f,i ,shd we;s by,u fldgi function head f,i ye`oskafjs'
Function definition tflys function head tl wksjdrAhfhkau return type tlskao parameter list tlskao
function declaration tlg iudk jsh hq;=h'

Function definition tl ;=, isoqjkafka l=ulao@ uq,ska addition kuska variable tlla idod we;s w;r
thg parameter list tflys we;s variable hq.f,ys w.hka f.a tl;=j wdfoaY lr we;' bkamiq return
keue;s keyword tl Tng olakg ,efns' return ys ldrAhh kus" th bosrsfha we;s variable tflys w.h
function tlska msg;g ,nd oSuh' tkus function tflka msg;g o;a; m%:`odkh lrkafka return keyword
tl uS.sks' return ls%hd;aul jSu;a iu`.u function tflys ls%hdjo ksud jS hhs'

kuq;a oeka function tflka return jk w.hg isoqjkafka l=ulao@ fuhg ms,s;=r ;j fudfyd;lska Tng
,efnkq we;'

3. Function call

fmroSo i`oyka jQ mrsos function tlla ls%hd;aul jSu wdrusN jkafka function call u.sks' oeka wm by;
add function tlg main ;=, isg call lrK wdldrh n,uq:

44

========================================================

void main()
{

int result = add(10,2);
cout << result << endl;

}

========================================================

add ys function call tl" Tng fmroS yuqjQ print function tflys function call tlska fjkia u.la f.k
we;s wdldrh fmfkao@ fuu call lsrSu isoqjkafka expression tlla ;=,oSh' fuhska isoqjkafka add u.ska
return lrKq ,nk w.h result keue;s variable tlg wdfoaY jSuhs' function call tl isoqfjk wdldrh
n,kak' add bosrsfha we;s jryka ;=, ixLHd hq.,la wka;rA.;h' fuh kus add function tl ;=,g o;a;
we;=,a lsrSfus udrA.hhs' fuh parameter passing hkqfjka ye`oskafjs' fuu ixLHd hq., function
definition tflys num1 yd num2 hk parameters fj; wdfoaY fjhs' tjsg add ls%hd;aul jSfuka" tu
w.hka hq.f,ys fftlHh jk 12 w.h addition variable tl ;=, .nvd fjhs' oeka return u.ska 12 w.h
function tlska msg flfrA' tu w.h main ;=, we;s result variable tl ,nd .kshs' wjidkfhaoS tu w.h
print fjs'

Tn by; oqgq function declaration, definition iy call hk wjia:djka" tl jevigykla f,i my;ska
fm,.iajd we;:

========================================================

#include

int add(int num1, int num2);

void main()
{

int result = add(10,2);
cout << result << endl;

}

int add(int num1, int num2)
{

int addition = num1 + num2;
return addition;

}

========================================================

45

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->