variable 1 of 2
play

Variable (1 of 2) Consists of: Name so we can - PDF document

2013-09-17 Variable (1 of 2) Consists of: Name so we can refer to its storage loca1on Lecture 2 at lower level converted to


  1. 2013-­‑09-­‑17 ¡ Variable ¡(1 ¡of ¡2) ¡ Consists ¡of: ¡ • Name ¡ – so ¡we ¡can ¡refer ¡to ¡it’s ¡storage ¡loca1on ¡ Lecture ¡2 ¡ – at ¡lower ¡level ¡converted ¡to ¡an ¡adress ¡in ¡memory ¡ – can ¡be ¡aliased ¡by ¡way ¡of ¡reference ¡ • Value ¡ – what ¡we ¡store ¡in ¡the ¡loca1on ¡ Func1ons ¡ – will ¡ never ¡be ¡empty, ¡not ¡even ¡before ¡we ¡fill ¡it ¡ • Type ¡ – size ¡of ¡storage ¡loca1on ¡ – interpreta1on ¡of ¡stored ¡value ¡ Variable ¡(2 ¡of ¡2) ¡ Constants ¡ Three ¡kinds ¡of ¡variables: ¡ • A ¡variable ¡can ¡be ¡declared ¡ const ¡ • Fundamental ¡(basic) ¡ • Modifica1on ¡of ¡a ¡const ¡variable ¡will ¡give ¡compila1on ¡ error. ¡ – stores ¡a ¡value ¡of ¡fundamental ¡type, ¡nothing ¡more ¡ • Object ¡ • The ¡compiler ¡can ¡treat ¡constant ¡variables ¡more ¡ – stores ¡values ¡1ed ¡to ¡an ¡derived ¡type ¡(struct, ¡class, ¡union, ¡enum) ¡ efficiently. ¡ – opera1ons ¡associated ¡to ¡the ¡type ¡are ¡provided ¡ • The ¡programmer ¡have ¡less ¡worries ¡with ¡constant ¡ – more ¡later ¡in ¡the ¡course ¡ variables ¡than ¡other. ¡ Big ¡benefit! ¡ • Pointer ¡ • A ¡const ¡variable ¡is ¡ much ¡be4er ¡than ¡a ¡literal ¡because ¡ – stores ¡just ¡the ¡adress ¡of ¡some ¡other ¡variable ¡ you ¡refer ¡to ¡it ¡by ¡ name , ¡and ¡change ¡it ¡at ¡ one ¡place. ¡ – requires ¡cau1on: ¡what ¡if ¡the ¡adress ¡ does ¡not ¡contain ¡said ¡ • Constants ¡use ¡upper ¡case ¡leVers ¡by ¡conven1on. ¡ variable? ¡ – more ¡later ¡in ¡the ¡course ¡ const int SIZE{1000}; References ¡ Pointer ¡basics ¡ • A ¡variable ¡that ¡store ¡an ¡adress ¡ • A ¡varaible ¡can ¡be ¡declared ¡to ¡be ¡an ¡alias ¡for ¡an ¡ • Declared ¡as ¡pointed-­‑to-­‑type* ¡ already ¡exis1ng ¡variable ¡ • Go ¡to ¡that ¡adress ¡by ¡prefixing ¡the ¡* ¡operator ¡ • The ¡exis1ng ¡variable ¡gets ¡a ¡second ¡name, ¡but ¡ • Get ¡an ¡adress ¡by ¡prefixing ¡the ¡& ¡operator ¡ is ¡in ¡all ¡other ¡aspects ¡iden1cal ¡to ¡the ¡new ¡ • Use ¡ nullptr ¡for ¡unset/invalid ¡adresses ¡ • The ¡binding ¡occur ¡only ¡once, ¡at ¡ini1aliza1on ¡ int variable{9}; int* pointer{nullptr}; string professor{”C. Kessler”}; pointer = &variable; string& clever_fellow{professor}; *pointer = 4; 1 ¡

  2. 2013-­‑09-­‑17 ¡ Sequence ¡and ¡block ¡ Func1on ¡(1 ¡of ¡3) ¡ • A ¡sequen1al ¡list ¡of ¡statements ¡ • A ¡block ¡that ¡has ¡been ¡given ¡a ¡name ¡ • Can ¡be ¡executed ¡(called) ¡by ¡wri1ng ¡it’s ¡name ¡in ¡other ¡parts ¡ • Each ¡statement ¡is ¡terminated ¡with ¡a ¡semicolon ¡ of ¡the ¡program, ¡ provide ¡reusable ¡code ¡ • All ¡statements ¡are ¡inside ¡some ¡block ¡ • Can ¡accept ¡input ¡(parameters) ¡from ¡calling ¡code ¡ • En1re ¡block ¡form ¡a ¡compound ¡statement ¡ • Can ¡give ¡(return) ¡a ¡result ¡back ¡to ¡calling ¡code ¡ • Statements ¡are ¡executed ¡one ¡by ¡one ¡in ¡order: ¡ return-type function-name (parameter-list) { // beginning of block { statement1; statement1; statement2; statement2; statement3; return expression; } } // end of block Func1on ¡(2 ¡of ¡3) ¡ Func1on ¡(3 ¡of ¡3) ¡ A ¡func1on ¡must ¡have ¡a ¡single ¡well ¡defined ¡task. ¡No ¡side ¡effects! ¡ • Mul1purposed ¡func1ons ¡are ¡hard ¡to ¡reuse. ¡ • • Func1on ¡name ¡should ¡be ¡well ¡choosen. ¡ • Func1ons ¡with ¡sid ¡effects ¡are ¡hard ¡to ¡reuse. ¡ • The ¡purpose, ¡inputs, ¡and ¡result ¡must ¡be ¡documented ¡in ¡a ¡comment ¡before ¡ • Ocen ¡seen ¡in ¡student ¡code. ¡ BAD, ¡BAD, ¡Threefold ¡BAD . ¡ the ¡func1on. ¡Also ¡document ¡any ¡assump1ons, ¡presump1ons ¡or ¡special ¡ considera1ons. // BAD BAD BAD (oh well.. ;-) // purpose: calculate sum of love // inputs: integers a, b // You can’t have one without the other!!! // output: the sum of love (integer) void love_and_marriage(int a, int b) int love(int a, int b) { return a + b; } { // purpose: print v on stdout cout << a + b << endl; void marriage(int v) { cout << v << endl; } } marriage(love(a,b)); // GOOD, individually reusable functions love_and_marriage(a, b); // can’t do just love!!!! // what’s wrong above? Func1on ¡types ¡ Func1on ¡declara1on ¡and ¡defini1on ¡ • The ¡return-­‑type, ¡func1on-­‑name ¡and ¡ • A ¡func1on ¡with ¡no ¡return ¡value ¡is ¡ocen ¡called ¡ parameter-­‑list ¡followed ¡by ¡a ¡semicolon ¡is ¡a ¡ a ¡ subrou=ne ¡or ¡a ¡ procedure ¡ declara1on. ¡ • A ¡func1on ¡part ¡of ¡an ¡object ¡variable ¡is ¡ocen ¡ • If ¡you ¡specify ¡the ¡en1re ¡func1on ¡body ¡(block) ¡ called ¡a ¡ member ¡func=on ¡or ¡a ¡ method ¡ instead ¡of ¡semicolon ¡it ¡is ¡a ¡defini1on. ¡ • A ¡func1on ¡created ¡inline, ¡or ¡”on ¡the ¡fly” ¡is ¡ • Declara1on ¡ called ¡a ¡ lambda ¡func=on ¡ – Tells ¡the ¡compiler ¡the ¡func1on ¡exists ¡somewhere. ¡ • An ¡object ¡possible ¡to ¡call ¡as ¡a ¡func1on ¡is ¡called ¡ • Defini1on ¡ a ¡ func=on ¡object , ¡and ¡have ¡operator() ¡defined ¡ – Places ¡func1on ¡code ¡in ¡program ¡memory. ¡ 2 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend