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

variable 1 of 2
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

2013-­‑09-­‑17 ¡ 1 ¡

Lecture ¡2 ¡

Func1ons ¡

Variable ¡(1 ¡of ¡2) ¡

Consists ¡of: ¡

  • Name ¡

– so ¡we ¡can ¡refer ¡to ¡it’s ¡storage ¡loca1on ¡ – at ¡lower ¡level ¡converted ¡to ¡an ¡adress ¡in ¡memory ¡ – can ¡be ¡aliased ¡by ¡way ¡of ¡reference ¡

  • Value ¡

– what ¡we ¡store ¡in ¡the ¡loca1on ¡ – will ¡never ¡be ¡empty, ¡not ¡even ¡before ¡we ¡fill ¡it ¡

  • Type ¡

– size ¡of ¡storage ¡loca1on ¡ – interpreta1on ¡of ¡stored ¡value ¡

Variable ¡(2 ¡of ¡2) ¡

Three ¡kinds ¡of ¡variables: ¡

  • Fundamental ¡(basic) ¡

– stores ¡a ¡value ¡of ¡fundamental ¡type, ¡nothing ¡more ¡

  • Object ¡

– stores ¡values ¡1ed ¡to ¡an ¡derived ¡type ¡(struct, ¡class, ¡union, ¡enum) ¡ – opera1ons ¡associated ¡to ¡the ¡type ¡are ¡provided ¡ – more ¡later ¡in ¡the ¡course ¡

  • Pointer ¡

– stores ¡just ¡the ¡adress ¡of ¡some ¡other ¡variable ¡ – requires ¡cau1on: ¡what ¡if ¡the ¡adress ¡does ¡not ¡contain ¡said ¡ variable? ¡ – more ¡later ¡in ¡the ¡course ¡

Constants ¡

  • A ¡variable ¡can ¡be ¡declared ¡const ¡
  • Modifica1on ¡of ¡a ¡const ¡variable ¡will ¡give ¡compila1on ¡
  • error. ¡
  • The ¡compiler ¡can ¡treat ¡constant ¡variables ¡more ¡
  • efficiently. ¡
  • The ¡programmer ¡have ¡less ¡worries ¡with ¡constant ¡

variables ¡than ¡other. ¡Big ¡benefit! ¡

  • A ¡const ¡variable ¡is ¡much ¡be4er ¡than ¡a ¡literal ¡because ¡

you ¡refer ¡to ¡it ¡by ¡name, ¡and ¡change ¡it ¡at ¡one ¡place. ¡

  • Constants ¡use ¡upper ¡case ¡leVers ¡by ¡conven1on. ¡

const int SIZE{1000};

References ¡

  • A ¡varaible ¡can ¡be ¡declared ¡to ¡be ¡an ¡alias ¡for ¡an ¡

already ¡exis1ng ¡variable ¡

  • The ¡exis1ng ¡variable ¡gets ¡a ¡second ¡name, ¡but ¡

is ¡in ¡all ¡other ¡aspects ¡iden1cal ¡to ¡the ¡new ¡

  • The ¡binding ¡occur ¡only ¡once, ¡at ¡ini1aliza1on ¡

string professor{”C. Kessler”}; string& clever_fellow{professor};

Pointer ¡basics ¡

  • A ¡variable ¡that ¡store ¡an ¡adress ¡
  • Declared ¡as ¡pointed-­‑to-­‑type* ¡
  • Go ¡to ¡that ¡adress ¡by ¡prefixing ¡the ¡* ¡operator ¡
  • Get ¡an ¡adress ¡by ¡prefixing ¡the ¡& ¡operator ¡
  • Use ¡nullptr ¡for ¡unset/invalid ¡adresses ¡

int variable{9}; int* pointer{nullptr}; pointer = &variable; *pointer = 4;

slide-2
SLIDE 2

2013-­‑09-­‑17 ¡ 2 ¡

Sequence ¡and ¡block ¡

  • A ¡sequen1al ¡list ¡of ¡statements ¡
  • Each ¡statement ¡is ¡terminated ¡with ¡a ¡semicolon ¡
  • All ¡statements ¡are ¡inside ¡some ¡block ¡
  • En1re ¡block ¡form ¡a ¡compound ¡statement ¡
  • Statements ¡are ¡executed ¡one ¡by ¡one ¡in ¡order: ¡

{ // beginning of block

statement1; statement2; statement3;

} // end of block

Func1on ¡(1 ¡of ¡3) ¡

  • A ¡block ¡that ¡has ¡been ¡given ¡a ¡name ¡
  • Can ¡be ¡executed ¡(called) ¡by ¡wri1ng ¡it’s ¡name ¡in ¡other ¡parts ¡
  • f ¡the ¡program, ¡provide ¡reusable ¡code ¡
  • Can ¡accept ¡input ¡(parameters) ¡from ¡calling ¡code ¡
  • Can ¡give ¡(return) ¡a ¡result ¡back ¡to ¡calling ¡code ¡

return-type function-name(parameter-list) {

statement1; statement2; return expression;

}

Func1on ¡(2 ¡of ¡3) ¡

  • A ¡func1on ¡must ¡have ¡a ¡single ¡well ¡defined ¡task. ¡No ¡side ¡effects! ¡
  • Func1on ¡name ¡should ¡be ¡well ¡choosen. ¡
  • The ¡purpose, ¡inputs, ¡and ¡result ¡must ¡be ¡documented ¡in ¡a ¡comment ¡before ¡

the ¡func1on. ¡Also ¡document ¡any ¡assump1ons, ¡presump1ons ¡or ¡special ¡ considera1ons.

// purpose: calculate sum of love // inputs: integers a, b // output: the sum of love (integer) int love(int a, int b) { return a + b; } // purpose: print v on stdout void marriage(int v) { cout << v << endl; } marriage(love(a,b)); // GOOD, individually reusable functions // what’s wrong above?

Func1on ¡(3 ¡of ¡3) ¡

  • Mul1purposed ¡func1ons ¡are ¡hard ¡to ¡reuse. ¡
  • Func1ons ¡with ¡sid ¡effects ¡are ¡hard ¡to ¡reuse. ¡
  • Ocen ¡seen ¡in ¡student ¡code. ¡BAD, ¡BAD, ¡Threefold ¡BAD. ¡

// BAD BAD BAD (oh well.. ;-) // You can’t have one without the other!!!

void love_and_marriage(int a, int b) { cout << a + b << endl; } love_and_marriage(a, b); // can’t do just love!!!!

Func1on ¡types ¡

  • A ¡func1on ¡with ¡no ¡return ¡value ¡is ¡ocen ¡called ¡

a ¡subrou=ne ¡or ¡a ¡procedure ¡

  • A ¡func1on ¡part ¡of ¡an ¡object ¡variable ¡is ¡ocen ¡

called ¡a ¡member ¡func=on ¡or ¡a ¡method ¡

  • A ¡func1on ¡created ¡inline, ¡or ¡”on ¡the ¡fly” ¡is ¡

called ¡a ¡lambda ¡func=on ¡

  • An ¡object ¡possible ¡to ¡call ¡as ¡a ¡func1on ¡is ¡called ¡

a ¡func=on ¡object, ¡and ¡have ¡operator() ¡defined ¡

Func1on ¡declara1on ¡and ¡defini1on ¡

  • The ¡return-­‑type, ¡func1on-­‑name ¡and ¡

parameter-­‑list ¡followed ¡by ¡a ¡semicolon ¡is ¡a ¡

  • declara1on. ¡
  • If ¡you ¡specify ¡the ¡en1re ¡func1on ¡body ¡(block) ¡

instead ¡of ¡semicolon ¡it ¡is ¡a ¡defini1on. ¡

  • Declara1on ¡

– Tells ¡the ¡compiler ¡the ¡func1on ¡exists ¡somewhere. ¡

  • Defini1on ¡

– Places ¡func1on ¡code ¡in ¡program ¡memory. ¡

slide-3
SLIDE 3

2013-­‑09-­‑17 ¡ 3 ¡

Func1on ¡input ¡

  • Zero ¡or ¡more ¡specified ¡in ¡parameter ¡list ¡
  • Parameters ¡are ¡variables ¡that ¡require ¡a ¡value ¡in ¡order ¡

to ¡call ¡the ¡func1on ¡

  • Also ¡called ¡formal ¡parameters ¡
  • The ¡value ¡we ¡assign ¡to ¡a ¡parameter ¡when ¡calling ¡a ¡

func1on ¡is ¡called ¡argument, ¡or ¡actual ¡parameter ¡

  • Datatype ¡and ¡order ¡of ¡arguments ¡must ¡match ¡the ¡

specified ¡formal ¡parameters ¡

  • Automa1c ¡conversion ¡can ¡occur ¡if ¡compiler ¡know ¡a ¡

way ¡to ¡convert ¡from ¡argument ¡to ¡formal ¡parameter ¡

Input ¡only ¡parameters ¡

  • Declared ¡as ¡normal ¡variable ¡when ¡of ¡fundamental ¡
  • type. ¡

– Arguments ¡are ¡copied ¡to ¡parameters ¡ – Efficient ¡for ¡fundamental ¡(small) ¡types ¡ void example(int normal_input);

  • Declared ¡as ¡const ¡reference ¡when ¡of ¡object ¡type. ¡

– Clear ¡to ¡programmers ¡that ¡object ¡is ¡not ¡modified ¡in ¡the ¡ func1on, ¡despite ¡reference ¡ – Arguments ¡are ¡referred ¡to ¡from ¡the ¡func1on ¡ – Efficient ¡for ¡object ¡(large) ¡types ¡ void example(string const& object_input);

Input/Output ¡parameter ¡

  • Declared ¡as ¡reference ¡variable. ¡

– CluVer-­‑free ¡and ¡safe, ¡compiler ¡create ¡binding ¡(alias) ¡between ¡argument ¡and ¡ parameter ¡ – Clear ¡to ¡programmers ¡that ¡values ¡passed ¡in ¡may ¡be ¡modified ¡by ¡the ¡func1on. ¡ – Arguments ¡are ¡referred ¡to ¡from ¡the ¡func1on ¡ void example(string& input_output);

  • Declared ¡as ¡pointer ¡variable. ¡

– Programmer ¡must ¡make ¡correct ¡binding ¡ – Code ¡cluVered ¡by ¡adress-­‑of ¡(&) ¡and ¡content-­‑of ¡(*) ¡operators ¡ – Allow ¡three ¡modes: ¡input/output/unused ¡ – Set ¡pointer ¡to ¡nullptr ¡to ¡indicate ¡invalid ¡or ¡unused ¡parameter, ¡the ¡func1on ¡ must ¡explicitly ¡include ¡code ¡to ¡check ¡for ¡nullptr ¡ void example(string* p_out){ *p_out = ”goes out”; } example(&some_str); // take address of some_str at call

Why ¡not ¡just ¡return? ¡

  • In ¡some ¡cases ¡you ¡need ¡to ¡”return” ¡several ¡
  • values. ¡

– Solve ¡a ¡2:nd ¡degree ¡polynom ¡(ax2+bx+c=0). ¡

  • In ¡some ¡cases ¡it ¡does ¡not ¡make ¡much ¡sense ¡

for ¡a ¡func1on ¡to ¡return ¡something. ¡

– Does ¡not ¡compute ¡something ¡from ¡input. ¡

  • In ¡some ¡cases ¡it’s ¡not ¡very ¡efficient ¡

– Returning ¡a ¡value ¡cause ¡an ¡extra ¡copy ¡opera1on. ¡

Default ¡parameters ¡

  • Parameters ¡can ¡be ¡given ¡default ¡values ¡
  • Specified ¡in ¡declara1on ¡only, ¡since ¡defini1on ¡may ¡be ¡

unknown ¡to ¡compiler ¡if ¡program ¡is ¡in ¡several ¡files. ¡

  • Default ¡values ¡can ¡only ¡be ¡specified ¡for ¡last ¡non-­‑default ¡

parameter ¡

  • Can ¡be ¡omiVed ¡when ¡calling ¡the ¡func1on ¡

void ignore(int n = 1, char stop = EOF); ignore(); // call ignore(1, EOF) ignore(1024) // call ignore(1024, EOF); ignore(numeric_limits<int>::max(), ’\n’);

Func1on ¡result ¡

  • Func1ons ¡evaluate ¡to ¡exactly ¡one ¡result, ¡specified ¡by ¡a ¡

return ¡statement ¡

  • The ¡result ¡can ¡be ¡specified ¡as ¡nothing, ¡void ¡
  • The ¡result ¡can ¡be ¡specified ¡as ¡auto ¡in ¡C++11, ¡meaning ¡

it ¡is ¡specified ¡later, ¡NOT ¡automa1c ¡

  • A ¡func1on ¡can ¡terminate ¡it’s ¡block ¡early ¡by ¡execu1ng ¡a ¡

return ¡statement ¡early ¡

  • The ¡call ¡to ¡the ¡func1on ¡immediately ¡evaluates ¡to ¡the ¡

returned ¡expression ¡and ¡con1nue ¡execu1on ¡at ¡the ¡ point ¡of ¡call ¡

  • A ¡func1on ¡automa1cally ¡return ¡void ¡at ¡end ¡of ¡it’s ¡block ¡
slide-4
SLIDE 4

2013-­‑09-­‑17 ¡ 4 ¡

Func1on ¡scope ¡

  • Formal ¡parameters ¡are ¡variables ¡local ¡to ¡it’s ¡

func1on ¡

  • Variables ¡declared ¡inside ¡the ¡func1on ¡are ¡

local ¡variables ¡to ¡the ¡func1on ¡

  • Local ¡variables ¡can ¡only ¡be ¡accessd ¡inside ¡it’s ¡

func1on ¡

  • All ¡local ¡variables ¡cease ¡to ¡exist ¡at ¡point ¡of ¡

return ¡

Naming ¡of ¡parameters ¡

  • The ¡name ¡should ¡inform ¡humans ¡of ¡the ¡

purpose ¡of ¡the ¡parameter, ¡so ¡we ¡can ¡specify ¡it ¡ at ¡point ¡of ¡call. ¡

  • Good ¡names ¡are ¡typically ¡5-­‑20 ¡characters. ¡
  • Name ¡must ¡begin ¡with ¡a ¡leVer ¡(or ¡underscore) ¡

and ¡contain ¡only ¡leVers ¡and ¡underscore ¡

  • Names ¡are ¡case ¡sensi1ve ¡

Overloading ¡

  • Different ¡func1ons ¡can ¡have ¡same ¡name ¡
  • Func1ons ¡with ¡same ¡name ¡should ¡be ¡iden1cal ¡in ¡

their ¡purpose ¡to ¡avoid ¡confusion ¡

  • Func1ons ¡with ¡same ¡name ¡must ¡have ¡different ¡

parameters ¡

  • Arguments ¡given ¡determine ¡which ¡func1on ¡is ¡

actually ¡called ¡(closest ¡match) ¡

  • Compiler ¡will ¡select ¡the ¡”best ¡match” ¡among ¡

func1ons ¡with ¡same ¡name ¡

  • Return ¡value ¡is ¡not ¡considered ¡even ¡if ¡different ¡

Overloading ¡example ¡

int triangle_area(int base, int height); int triangle_area(int side1, int side2, int side3); int triangle_area(int side1, int side2, float angle); int triangle_area(int side, float angle1, float angle2); triangle_area(1, 1); triangle_area(1, 1, 1); triangle_area(1, 1, 1.0); // which is called? triangle_area(1, 1.0, 1.0); triangle_area(1, 1, 1.0f); triangle_area(1, 1.0f, 1.0f); // can you add a default parameter to second version?

Alternate ¡func1on ¡syntax ¡

  • Some1mes ¡you ¡want ¡to ¡deduce ¡the ¡return ¡

type ¡from ¡input ¡parameters. ¡

  • Parameters ¡must ¡then ¡occur ¡to ¡the ¡compiler ¡

before ¡the ¡return ¡type ¡

  • Solved ¡with ¡auto ¡and ¡decltype ¡in ¡C++11 ¡

(both ¡is ¡also ¡useful ¡in ¡other ¡contexts) ¡

auto sum(float a, int b) -> decltype(a+b)

¡

Lambda ¡func1on ¡

  • C++11 ¡allows ¡lambda ¡func1ons, ¡or ¡”inline” ¡func1on ¡

defini1on ¡

  • Covered ¡in ¡detail ¡later ¡in ¡course ¡(when ¡useful) ¡

¡ Pos origin{3,4}; vector<Pos> data; sort(data.begin(), data.end(), [&origin](Pos a, Pos b)->bool {

return dist(a,origin) < dist(b,origin); });

slide-5
SLIDE 5

2013-­‑09-­‑17 ¡ 5 ¡

Rvalue ¡references ¡

  • C++11 ¡allows ¡rvalue ¡reference ¡parameters ¡
  • Covered ¡in ¡detail ¡later ¡in ¡course ¡(when ¡useful) ¡
  • Choosen ¡when ¡argument ¡is ¡temporary ¡(rvalue) ¡

void print(string const& s); void print(string&& s); string text{”Hello World”}; callme(text); callme(text + ”!”);

Facilitates ¡divide-­‑and-­‑conquer ¡(1) ¡

  • It ¡is ¡easy ¡to ¡check ¡if ¡a ¡chess ¡queen ¡may ¡move ¡if ¡we ¡know ¡how ¡

a ¡rook ¡and ¡bishop ¡may ¡move. ¡

  • Then ¡just ¡con1nue ¡and ¡implement ¡the ¡missing ¡func1ons. ¡
  • Note ¡that ¡func1ons ¡make ¡your ¡code ¡more ¡readable! ¡

bool is_legal_queen_move(Pos from, Pos to) { return is_legal_rook_move(from, to) || is_legal_bishop_move(from, to); }

Facilitates ¡divide-­‑and-­‑conquer ¡(2) ¡

bool is_legal_rook_move(Pos from, Pos to) { return is_horizontal_move(from, to) || is_vertical_move(from, to); } bool is_legal_bishop_move(Pos from, Pos to) { return is_diagonal_move(from, to); }

Facilitates ¡divide-­‑and-­‑conquer ¡(3) ¡

bool is_horizontal_move(Pos from, Pos to) { return from.y == to.y; } bool is_vertical_move(Pos from, Pos to) { return from.x == to.x; } bool is_diagonal_move(Pos from, Pos to) { return abs(from.x-to.x) == abs(from.y-to.y); }

Recursion ¡

  • In ¡math ¡you ¡use ¡induc1on ¡as ¡a ¡way ¡of ¡thinking ¡in ¡order ¡

to ¡prove ¡a ¡theorem. ¡

– prove ¡the ¡theorem ¡for ¡one ¡case ¡ – prove ¡that ¡if ¡true ¡for ¡some ¡case, ¡it’s ¡also ¡true ¡for ¡the ¡next ¡ – by ¡way ¡of ¡induc1on ¡it’s ¡true ¡for ¡all ¡cases ¡

  • In ¡programming ¡we ¡can ¡use ¡a ¡similar ¡way ¡of ¡thinking ¡to ¡

get ¡a ¡result ¡

– calculate ¡the ¡result ¡in ¡one ¡specific ¡input ¡ – write ¡a ¡func1on ¡that ¡calculates ¡the ¡result ¡ov ¡any ¡input ¡ given ¡the ¡previous ¡input ¡ – by ¡way ¡of ¡calling ¡itself, ¡the ¡calcula1on ¡get ¡to ¡the ¡specific ¡ input ¡and ¡calculate ¡the ¡result ¡from ¡there ¡on ¡

Recursive ¡defini1on ¡

  • A ¡recursive ¡defini1on ¡have ¡two ¡parts ¡

– A ¡star1ng ¡(or ¡ending) ¡point ¡ – How ¡you ¡get ¡from ¡one ¡to ¡the ¡next ¡(or ¡previous) ¡

  • Consider ¡N-­‑factorial ¡

– It’s ¡possible ¡to ¡express ¡by ¡way ¡of ¡itself! ¡ – 0! ¡= ¡1 ¡ – N! ¡= ¡N*(N-­‑1)! ¡

  • All ¡computa1ons ¡can ¡be ¡wriVen ¡recursive! ¡

– Some1mes ¡it’s ¡much ¡shorter, ¡simpler ¡and ¡clearer. ¡

slide-6
SLIDE 6

2013-­‑09-­‑17 ¡ 6 ¡

N-­‑factorial ¡two ¡ways ¡

int factorial(int N) { if ( N == 0 ) return 1; return factorial(N-1); } int factorial(int N) { return prod_from_one(1, N); } int prod_from_one(int X, int N) { if ( X == N ) return N; return X*prod_from_one(X+1, N); }

Exponen1a1on ¡

// xy=Pow(x, y): // x * Pow(x, y-1) if y>0 // 1.0/Pow(x, -y) if y<0 // 1.0 if y=0 double pow(double x, int y) { if (y > 0) return x*pow(x,y-1) else if (y < 0) return 1.0/pow(x,-y) else // y == 0 return 1.0; // Gets here eventually! }

File ¡separa1on ¡

  • Related ¡(cohesive) ¡func1ons ¡can ¡be ¡gathered ¡in ¡one ¡file ¡to ¡form ¡a ¡
  • package. ¡
  • A ¡package ¡can ¡be ¡compiled ¡separately, ¡and ¡do ¡not ¡need ¡

recompila1on ¡unless ¡you ¡change ¡a ¡package ¡source ¡file. ¡ ¡

  • Public ¡declara1ons ¡are ¡place ¡in ¡a ¡header ¡file ¡*.h ¡
  • Defini1ons ¡are ¡placed ¡in ¡a ¡implementaton ¡file ¡*.cc ¡
  • Header ¡and ¡implementa1on ¡files ¡should ¡have ¡the ¡same ¡name, ¡

except ¡for ¡the ¡extension ¡

  • Header ¡file ¡must ¡have ¡a ¡preprocessor ¡guard ¡to ¡protect ¡from ¡

mul1ple ¡inclusion ¡ #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ // public declarations #endif

To ¡be ¡con1nued. ¡

This ¡page ¡is ¡not ¡lec ¡blank. ¡Inten1onally. ¡