Partial preview of the text
Download PPS notes(programming for problem solving) and more Study notes Programming Languages in PDF only on Docsity!
VeebVUvVLVUVUUVUUUUUUUUUUYOUUUYUUUUUUUeeee fF UNIT-IL Functions £ ARRAYS Sllabus : Functions Wer difuried functions — function ieee Pe GSh Value 2 cable by Suference oe Classes Recursion . ARkays > Pntroduction ~~ Stogle dime nsional a 2D- aor Multi- démenstenal ay 7s Applications , “é ~~ or ( tunchon 5 A Sunchin taal ey Contained. block of Statements thal as Contes outa Spectfic , ewe ll epee task Or operation J priate) Poferms tating Qferation on the. Monttor. % S97 () Performs Square ot gf a ee no. den tefrcation of Joined ian ° wy PPP Ly p/p — the Ten tiftccction of function &¥ a Set of Paren thests > fe, apt, mame, ff theye %& a Set of Paren thesig then th ¥ Ee 20 foe Byrling ae display ¢ Sactorfel t ) * Types % Function, » functions ave heporteedd Pity 2 ae 1) Spstem— defined Function» the Function, that are alvead™ ES defined tn the System ave known ag System deftnec) Functron Fo Pantecy , Sete) Stan ¢ » GEE = These Fanetions ave also called ag lebra’ these Functions are Present header files : GH Stdfo-h Conio -h =3 math-h y DSer- detined Function: the functions that aye defined or Created by the user to Perform user-defined fenctong &g: Sumc) alsplayc Steps to create a Functron 1 Sep 1. Prototype of -Punction « Before | the Horscien name Shaded be declared above the moin Function And! * 4 known ay Preto type g Funchon. FEF AHDSA roe eon HF Ca functions because &) hig O0n Operakong One known PIAL IIDS LAA Function ac Pre mes Valusy to functions + functions can poss Values from Calling tunction tp the called function , Actual parameters; the valuus that ove passed om the cali Puncditn tee liven a Actual parametey (07) Ang uments 4 ~ “These 2 © onguuments may be Constant value ( 07) Vortaltey (oz) Formol_pavameters : The vartableg that are declared fh Med | Function are known as Pymal parameters i Syntax af Called function + Yeturn type function name (“ype Van 1, type vane, tye von3 ) cor ; ; 3 Not: D) no. achual ong temnents Should match wth the no. of Formal AUER IEE ‘ 2) Even ve Should match wth type and order . 3) Prototype mT Function Should be Same as that of called functé headey - PP Pp ageeeyry fis Pn Pah pie votd fun (End 2a) main cy a fat fee Pz100 ; y void Fun (Int 2) 7 PEC \n the Value >/.c) 2); } 5 PPP PPP pp ppp pp pnt pe Functions yetuyne valicog : the Functions Can also yeduyn vyalusy trom called function +p -the Place Of tre Function call tr the ees Function. thiy tan be done using Yelurn Std fn c. an Yeturn (value) » > TF function fo Yelurni, a value, the concerned goer Kesucord Should be Ge before the function Name of Yeturn type eh LP the Function ¥ esr Gergen value then the Yeturotepe Should be ft - ee? Pune on fs not yetuns, a value then the yekon tof ¢ the Soret, Should be af =e A vetur sil can yeluyn o One Vole . eee or ee terminate the fur chim en ae | Soe Gbdin-hs fit Sturn Cint x, tnt ee Void mary 6) t tet azlo, b=20, C: C= Sum (a oS i liga 5 fnt Sum Cinta, try) tnt 23 Z2= ie Yeturn (2), 5 VVVLVVUVUVUUUUUUUUUUY CUCU OUYYYY EYEE S ee CLVLVLULVUVUUVUYUYUUUUUW UN Od dod doe 2) No Povameters wlth veluyn “ype : Oe Vefd Sum ¢) Main Cc) ty Cova tend clyserc); C=Sum C) PEUS Gleb / cy) 3 fot Sum () ft fnt Ui Ghz 5 PE(“enter % £ 4 volueg:) , SPOS dsi.d¥ fe, 059 2=%+y; Feturn(z): ee Pavameters , No Yetuyn tyre : # fnclude < Stofo-hs Vofd Sum Cab / ink) MGinc ) ¢ tnt a pe 3 clasexcy 5 Piers a2 SP Cire td" La, values: |). Ly), Sun 6 ay); a): yrtehe Weta Sum Cint a, Int b) Inc; Czatb; pe (“Sum = aon 8 . é t) with Parameter. , with Yeturn tye. a Taelude < Stdioh> OE Sum Cn, toh ) Main cy { Tht abc; BEC ols ab Values: ") ; SO g indy, Vas. 4 a,b). Pe (Quip +o)" =o)" Gy; a 3 fot Sam (tot «, nb y tine ay Za a se a Paramertey ¢ : ee Some We Can achieve tran “ype "I Pp ‘ ’ 4 value (or) copy by valup . P 4 Yeference (oy) poss 7 x a b : a d re. Ts een | Pa. i ee ie n a7e Sent to Called Function Chief, by Forma Porameters ¢, that ay a ee Mo, cA. formal argdments toll note a ee - ‘ ; ted 4, achiral ogee ce F & LL LILILILIDIIDIIL IID DD DEOL OD DOO ODD Ow & o | oe Cnt ¥P, tat “temp ; fp: $p; *P =; *q = temp ; 3 ™Matn ¢) fnt a,b. A=10, b=10; PE" b ove Sw Socp (ha, Lb); WAP +p Fonplement paws by Yderence \ fnt ¥4) ao | ow py aat-d, beyd' ab) : ~ ofter Spe) a=S-d, bed ab): Recursfon PS ESS. ie Process by Lohfch anton calling Tah Such qa action & Called aq FOCUurSive function he Vofd matin Cc} { nie) as Void AC) = s <2) 2) a ar by) tot yo; ; Main c) 2 / Helle f , Hello PP(* Hello): Helio. if (283) AsAt) .- 3 3 Main oO" tl a PAPA LADIIPLRDAPPIPUVITGIADPTISSSASASE33 UUTesve Ve eLUUUVULUUUUUUUUUUUU CODCOD CCC CERES >) srdnel hs factorial of Be given no. csi Recursion 9 tt —factovtal (tnt ays VEd moiine deme, Int tact ; PEC“ entey no: sy ee ey &n) Fact = tactorfal (n); PEC meee of 7d & =/d" 0, fact); Gtho: 3 fat factorial (int 2) t ae 21) Yeturn (1); Clse 5 Local 2 @lobal vaviabley oe Tetury ( Xa #factoyinl C 4-1); local Vartables + these are the variables that are declared with tm the Functions . the function, 4, whch, 27 are declared — ai Void maine fot a=io; PF (“azy.d . a). Si lec) vs 3 oP Void Samyple ¢ fat bz20; PE US ben/d thy, 3 Cand” ai eee ae - Soc ea ~? To declare @ varfable Inc, the beopiete aia : Storage S clan Sialpiey debatype Ro Nace Evalue]; | ee oe, (hy cher, Pleat abuble Quale tte y 2 Stoned , a a bong Short . Stops class 5 Qutr, Static Cater , vegtstey Sete Storage Cl i ry Specifies Scope b te 72 ventabte, SPE: Scope of varfabl % Varfable Veber vvuesce € mean, the ane sf ov place wher Can be used oy Caccess ed. life ; life of varFable Means the Ppeitod | time th. vartalle Cust fh er 5 In ¢ there Gye by Se Classes 5 they Ove 2) Automale, Variable, 2» Statte 3) Registez 4) Exterp u 2) Aucbomedte Vartabley : : Kejoord t Aut ee RAm >) eae al al defeired hth tra funckion Automatic Vays, Gnbe ued oy Qtessed Funct on & whith, are chee lared auto ink %=I0 VMI local vat ates declared ufth tn Punch on ane ;by a automatic Vortalles . : = Ve vLUVUUVUUUUUUUW UYU Odd 2) Stotte Vantably, Konrad 4 Stottc Memory ; RAm D Name volue > 200. a Htedee vontabt, ald teePenileed” Moulillbe accrued tm Qn the Same function am whith they have been cleclared Stmtlar +p the automabyc Vartalley . PTH AAT SS 4 avabat aa ee olf fevence. & Sledéc vartaltes donot loose it's value Cven often. the function fn whih % 4 declared terminaly © D Static vartable fll be fnétalfred o once, PE ik g nok « tntttalfred wfth ca value , then ig OR fnittalired with Adoult valut two. ee eee Votd democ ) f Static int Posies Print? (in Peyd) Pp). Potts a 3 dp: SF 3s) Yow os 2 PAPA LADADAAADAAABSASSIANDTSA G file e =H tnelude ; PRC as 7.4 ' col dlp C2) PY ("tla valu of 21d). 0), ene: 3 files.c extern Int x: Void oe O ap: lo lo PEC“ the value q Lesd |x) X= A410; LILA APAILAEAAAAPAAAHP APRA FS 4 = PE proceso pre Co Directives : > The preprocessor 4 a ppm that Prcesses es Gyre Cats ae © PE fs passed +o the Comptler . PE operates under the Contre 4% Preprcessor directives . = Preprocesscy Ofrectives ane placed GOS Source Pregram bebe the mai line . — Directive ton be divided tnto 3 Calegortes. 1) Maw Substitution directives . 2) Pele Inclusion oltre ctives , 3) Condé-honal Compilation . 9 APPPAIADID 4-4 8) Matw Substttutton : Macro Subslttulton ly a Process whew an fdentfitey fra program fs veplaced by a shag Sytan : # define Pderti-ftey strtog i fai eye ne. Pr 3B.ly a a float area, ¥=6.3; Qvea = PLHVET: PR ("area= 7-f' area): 3 q 2) # define area (x (P14? #2) maint ) t Ffloak 7-62, %=6.,a; a= Area (7,); PEC" area = has) a= Aven (7) Oe ce u) file faclusion » ele Theluston ofrectt ves Cawe one File to be frcluded fn another fle . Syl: Hh tocluce “ felename' 4 Frm exletnal vartalley . Y) Condltttonel Cormptlatton + Dp we want the Compfler foskep over Pond f a Source Cede by ust oF tale and -#Hend f Mile Commands which have the -followeng Peet ai tt fle raatrename Slt 1; Stmt 9; ae ae 4 CeVLUVEVLEVVUVUUUVUUUUUUUOOUDOUDOCOCeOCeCeEs “" Lio ARRAYS Clement, ¢ Same ~ deda Pe Cor) Array can also be dleffned a homogeneous Collection > ante Grouped with a Stmelar name ~ => ”n par & a derived datatype S One demenstonal (oy) Stihle dimensional ‘aidter iff -D) S Seam get i » » + Definition: a ee ba Staed Stee Segprenced collection 4 >» >) > d values , whith. Declaration L Defiz.ttion wv d oe Arrow oy, array must be declared and dined bebre ut ton be used. GS pe rere [Pag “hrrag declaracton tells the Comptler , the name of the Suog, the type of Cath element and Sre or no.af elements acres. ae tnt Stores [10] Float Salaries (10), a fo Sinpay Sire =6 byly - pea arsine : no} - routs sft palpi oe Negere =a) nj - us « and i" u Nilay = i # gz —? Accessing elem i ' coe. i ents fra Arrey : fle US an index to aceey trdttdual climenty ty seek etry inden Srrally Staucts with ‘rey. Gi fnt als). Ase ela — 4f07 LUV ULUVUVUUNUUN NYU OW Tnttralt: ation q ede Y a) sae] ‘tnttialfration Can be efermed fr ley cig ) Bask Snitialiration : ak tnt no [5)= {3, 9; 12/14 ,)62. w) Thitialfiratin without Site. + a fet nol Je { 2, Hult, 4 Bf 5 Poh the above coum Ste 5 emitted, $0 comptla alle enough Space fer all thea lined! ehuments | 40) Partial fnttralircatin D SRP L SS A fat num( 5 = £3,935 PRematnirg elements on felled wlth co's wy w/ 1) Ructime ‘intttaliration » Another way to fill the Chey ty read the valus #; the keyboard T The method of npubti eid lie wiitiwrerarrie do» BAP LAAALDIALDAABSASS valis Canbe done use; most PPro priate loo tp fil the arn a for (i207 tes » r+) FY 4 bru (7) ; Pibavcn, values . = as the Con 1) main c) a loop. the Yay loop ; tents ff the ALA Can be. J 4 hae Via 1 (0, 20,50) BO" Yd", ate]): al May afi): PEC" yg ala)) | 3