-
Ir 購臨, lntroductlon to⊂Programmlng 一Fun⊂tlons Global Var昏abies- Wa5臼da UniverSbty
-
SLLdi,- Today's Topics i n Creating Functions - How to define e function - Ca[ling a function 1 Loeal varrables - Seope ofa lecal varsable t Use global vartables 1 declering a g[obal vanable - Scope ef a g)obal vanable - Arrays as global variables
-
1 1 #lnclude (stdlo h> fYpe-of-functton Name(Parameters){ Oescrtpt2on returz Ret=rn.value, fkGwe the return valae ot this functien*/ } int main(veid){ deuble a; a = "ame(Parameters); /*Cal1 to the functlon "ame*/ } -diL 1 N 1 N t N 1 Each function has Parameters and Retutn value, Define a functfon before #mclude stntement after main function (Nft)u can use the funct-on after you cief-ne it ) Funct-en name should not be the same as reserved words Funct)ens exit wtth the return statement Multi parameters are acceptecl but the return value should be ene When we cal[ a funct-on, ass)gn Parameters te the funct)en and get a return value
-
kL l 1 1 1 =bLL/ lo;Lveilra: maln(vold>{ deuble a[3] a[O]=2 5*Of! a[1]=2 5*t+i a[2]=2 5*2-! return O; Avoid N"1infi o; o o m)ny Ltmcs the comf"on pJrts '!5p LO" i 1
-
' tl tH .',.' 2Rt Lgii kfi/ .) h- App:lutigps FEz[es systern nlE Ed/t opmops euffEG fools btsRn-zz ・. ee ,5 ur C Help in' kn e m n - lwork - aj/ 1 e ma @ :.tt/ /t/t .'tt o :"t ineinttreldH elovbte t[]l slels1S.th stttT:5Tr HtZln!Sll tet"en di ] L +le 41e. +le" es PubLic Templetes Vldees ge weik b ' ev:LMYTM lceTe te Te felle- Tn gut e -zz .GHU I n ENU Erscs ere ce'p4re't et t"e s llnk [ll[k Heuse 1 en lt br petttellw enteteti cnmmariti tvp" E"nes' TopLl tFuhti:me"taL: Gr'UILtru= meVe te lt Cnrlttnl g ...t ope'et:'9 snd tvP" 5yster kEr 1-
-
vX- More remarks on functions 1 N AII programs of C language eonsist of functiQns J xnt matn(veid){,,,} is also a functien, - Every program need the rnatn function l fypes ef functtons - ma)n function (called at the begtnning of the program) - Standard functions (with #inelude statement) - Functdons for obtalndng the return value (ex sm(x), log(x)) - Operetions in the functions with ne return vaiue (ex printf(''Hello, worldNn`'), etc ) l Why making functions7 - Functions allow us to group commonly used code inte a cemnect - We cen write e progrem in units easy to understend unit i
-
塾 Exercise A 『
-
s vL- A hint of exercise i / N Ybu make a functien max(a, b) that returns a btgger va[ue of a ancl b We ca[1 thts function in the matn function, 1 double max(double a, deuble b){ if Ca statement is needed, (2) -im option ls necessary / /
-
s 1 / / A hint of exercise Here ts an example of the main function the mein functien int main(void){ double x, yd Pnrrtf("Znput x:ii) scanf('dXl±",ex) y max(x, -x) Y Max(Y, XIX) prmtf("Answer retuTp O; } l - - im",y) A variabie x is an lnput value The y is assigned a temporary value of the maximum Call the max functien repe2teclly and assign e2ch return values to y -diL
-
' w le t.. .-ag/,VVT/=Z.-v, tc/ n) tt n" App:1catsgns FEH[es systern nle Ed/t opmaps eufieG Tbols bgmo sc -E ・. ea ,s ur av c HeLp sc ke st di iwork -q, ge mu av ' :±tt utt .'ld.u //. Lrit ine=nttrbldH do"ble t y, PUntfi I"put x s[snft/ lf .evi. Y- inaXtS X), pr:ntit Answer ts Fet"op e ] , Hf Ll, y) es PubLzc Templetes Vltiees wetk fe ' u I s tas : lce e te Gr'U E'scs 4re ce'pere't ef t"e Er'UILtrur Te fellw-sllnk dl[k Heuseten lt br meVe te rt Tn "ult h pettlehlv enteteti cnmmariti tvpe Cnrlttnts g SS .GNUinhc=- TopLl tFuheilme"taLl------- 4pe'ett'9 snd typ" SY5ter flEr. 1-
-
em ...LrLttM!MT 1, V, tcl r') ' - aj, "E4 - lwork .MS nlE Eri/t opmens suffersfoo/g c Help d tsde sc e!s,' ts)eb iSwaMkNl@ttt 'esPtibl:c Templet-s Vzdees iweik =/ tttttttt t/tth lt deubleJex WeHble edouble bH lftetbl tEtu tmb } "U.eretu rna tnt Tslrit veld){ doublEx y, prt"tH ln;/ut x')t scanfl tlf, ex) 1 YJMS= [XII ts prtntH A/b-lyO r±sNtSH,y) ' ] .etu.-e v ' 1[omete GNU Emecse"e To teUtr- eUrik eUckMo"se [onPenent1 opEtetlngSYSteMbriSterrpevetoitaridtypeHET ' Te "u.Ltn perttHllv enTered cemmendtypeCanlrelQ u-t ,-E-ti D-cs- TmpLl /EundT"ent.t/................. tttt tt ! -
-
. . , , 国 圏 ■ 9 同IE ■ Ed「: opaen~ B口ffE ]畠圃sc_ 自 //c乳鴇Lt2 ’ 「dLP 卜Lン ‘ユn【1」幽¢e {m 可h h 目 dqubl巳 叩AX【凶o団b篭已 司 i「 にaqb} retUrn b eZ5e 「o「u【n o } 1rit m』工n【》Ndレ{ dOtabl日 M yρ pr±ntt【 Inp印L x scepf〔 、1「 」醇t}「 y訂而gx‘¥t x}r yロ 而h:‘y ㌶tX) y ■ MAX‘y 5『r電〔f3b prlnti【 AR5囚e「 ⊥! 「Etu「口 3」 } 「 匪ke 巨仙t Vlev Seerth derrnLmaヒ Help rw50S696@IO⊂alhost WOrk]$ 9⊂C■ W503695◎IOに自1hu5t伽r田ork 一 口 Pt 「 【 1㌃ .「罰 .■肉 !.r ← 囲 臼Lho5ヒr LO閾ゆolhD畦b5軋 II 画
-
i / / 1 Parameters and Variables N When a value of the parameter is changed in a functien, the input value is never changed Ln the main functJen " Nbu can cleclare a vanable of the same name in dtfferent functions but these are considered as a dlfferent thing #mclude vozd func"nt x){ /* x x=7, !* The } mt main(void){ !nt x=3, tunc(x>, 1* Paraneter x pnnti("'x ls Xd.linL x); return O; } is deelared only in thzs function *1 x belew zs dlfferent :rem thls x, *f :s 3 and copies 3 te the x above, *1 1* Here "x is 3,'' *1
-
e nlE d ew tt Ed/t g ' opaeps ma ee 'rvedity ]. tr1 erna[s@tocRlhostlo[alelomaln euffeG taolg C Help - zz ・. be' kmn di -[xlwork /c .M ' s /tl ' , ma av r :±n[//Jtdtdte/ voiti fb"cU"t xH x-T ] tntmel"[VeldH tht x-1 fupctx} pr:nttVx"td ret/epm e ] Sri'/xl b ' U I Te Tn zz [ um ZCeTe te feUon gdlt e -GHU L G'- Erscs e'e ce'pore't 4t tbe Er'UIL:rux s llnk [ll[k Heuse 1 en lt br meVe te lt pettlallw enteteti cnmmariti tvp" Cnrlttdi g Emn[s- Top Ll tFuhtiame"taV: - - epe'at±'9 snd type 5YSter. ktr. 1- wnveseeegeele[alhastr- wa Em:ts@doca/hosL/ota. cu e
-
v Tt . . . rrv"AIZXiZ,' "I Appliatians PtH[Es -. e nlE Eri/t opaens eufie d ts an sc :tn[ludE tstete L/t system wr ,) E sw ptt/ Nle Sd/t Vlept Seerth knn,ohL Htlp rwSOE69Salocalhostworkl$ w50s6sEodocllh"st./worlt man lce e te Er'- E'scS Te fellon s 11nk d v Tn "ult A peteleliV U ts 'GHU emnts` I gcc sl v,ieEfigfi@lota/hnst- eemics@Jo[elhost/e[a 4 " a, Lee MopAprl] 1]sAM wsosfisfi .u" / wi as
-
s 1 1 1 Scope of variables 1 The scope of a varlab[e ls the area of the program ln whTch the vanable is valid N A local vanable (includtng parameters) has e scope that is limited to the function in which tt is declared - Ybu can declare a local vanable wtth the same name as anether lecal variable in a different functton, These are consiclered as different, double ±unct±onA } :nc funct:enB(xnt deuble y, } (mt x, irrt y){ /i The scepe e± x and y. +1 xi{ /lp Tlie scepe e± x ana y. *1 i
-
"LLL- Global variables =>A globa] var-able ss valid everywhere, so its scepe ts the who[e l Declared outside of functtons (usually after #melude or #dehne statement) - Scope entlre program 1 / progrem #mclude double ParamA, ParetuB: /. 1, Glebal variables sk1 deuble XinearCdeuble x){ return ParamA*x+ParamH; f* 3. Global can be used here- *1 } lnt maln(vold){ ParamA=5 0: Pa:amH=3 2: ls 2 Ass:gned values t/ p:int±C'"Result zs X±,fu'". Iinea:(1 2)): pnnt±("Result is X±,IEn", lmeaT(2,O)), retum O, } i
-
s VtL- Arrays as global variables 1 / Huge arrays must be declared as global variables ex mt main(void){ double Array[2000000000] !* Huge array camot declere - - Arrays as glebal variables are instialized all The huge array is net recemmended unless double Array[2000eOOOOO] = {1.0, 2.e}; o it arrays, , ts nece$sary /* Ne way.,. */ i
-
s "VLL- Examples of functions 1 : / 1 1 int methodl(mt a, int b) - Parameters a, b Ctype ef lnt) fype of return value znt doub!e methed2(int a, deuble b) - Parameters a, b (type ef int and double respectweiy) TYpe of return value, denble boolean methed3Cint a, ini b, int c) - Parameters' a, b, c (type of int) fype ef return value blDolean (true or talse) void method4(double x) - Parameters' x {type ef double), fype eF return value nbne double methed5(vetd) - Parameters, none, 1lype of return value, deuble i
-
,,llll)Ui---- Summary -dik.- 1 N Creeting Functions - How to define a functton - Calling a function 1 Loeal vamables - Scope ofa local vanable t Use global vartables 1 declaring a g[obal vaneble - Scope ef a glebal vaneble - Arrays as global variab]es