csc 107 – programming for science. today’s goal discuss how to hand data to functions review...

29
LECTURE 18: PASSING BY REFERENCE: EVIL GENIUSES OF PARAMETERS CSC 107 – Programming For Science

Upload: clare-flynn

Post on 20-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

LECTURE 18: PASSING BY REFERENCE:EVIL GENIUSES OF PARAMETERS

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Today’s Goal

Discuss how to hand data to functions Review loopholes in variables & scoping

rules Ways to get parameters to do whatever you

want How to use parameters and what they are

Page 3: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variables

Variable name location to store data Only for humans; 0x7E8A2410 harder to

remember Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

But what if something else modified memory? Variable's value "updated" without an

assignment But we have no control over these

location…

Page 4: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variables

Variable name location to store data Only for humans; 0x7E8A2410 harder to

remember Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

But what if something else modified memory? Variable's value "updated" without an

assignment But we have no control over these

location… YET

Page 5: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variables

Variable name location to store data Only for humans; 0x7E8A2410 harder to

remember Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

But what if something else modified memory? Variable's value "updated" without an

assignment But we have no control over these

location… YET

Page 6: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variable Scope

Variables not universal, lifetime limited by scope

Once declared, variable usable only in braces Scope defines lifetime where memory

location used Marks memory location "free" when scope

ends Cannot use outside scope as name will not

be known

Page 7: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variable Scope For EVIL

Variables not universal, lifetime limited by scope

Once declared, variable usable only in braces Scope defines lifetime where memory

location used Marks memory location "free" when scope

ends Cannot use outside scope as name will not

be known Unless we could access memory

location directly Variable is convenience to access

computer's memory

Page 8: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variable Scope For EVIL

Variables not universal, lifetime limited by scope

Once declared, variable usable only in braces Scope defines lifetime where memory

location used Marks memory location "free" when scope

ends Cannot use outside scope as name will not

be known Unless we could access memory

location directly Variable is convenience to access

computer's memory

Page 9: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Variable Scope

int findMin(int num) {int temp, min;cin >> min;num = 1;do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != 100000);return min;

}

int main() {int small, num;small = findMin(num);cout << "In: " << num << ", min= " << small << endl;return 0;

}

Page 10: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

int findMin(int num) {int temp, min;cin >> min;num = 1;do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != 100000);return min;

}

int main() {int small, num;small = findMin(num);cout << "In: " << num << ", min= " << small << endl;return 0;

}

Variable Scope

One name-but-

two memory locations

Page 11: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

int findMin(int num) {int temp, min;cin >> min;num = 1;do { cin >> temp; if (temp < min) { min = temp; } num++; } while (temp != 100000);return min;

}

int main() {int small, num;small = findMin(num);cout << "In: " << num << ", min= " << small << endl;return 0;

}

Variable Scope

One name-but-

two memory locations

Page 12: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Parameters are Variables

Just like variables, they name memory location Get new location each time function is

called Value stored at location changed by

assignments Unrelated to other variables even if names

overlap Parameters initialized to value in

argument Copies value into memory location created

for param Assignments affect memory location for

parameter

Page 13: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Parameters are Variables

Just like variables, they name memory location Get new location each time function is

called Value stored at location changed by

assignments Unrelated to other variables even if names

overlap Parameters initialized to value in

argument Copies value into memory location created

for param Assignments affect memory location for

parameter Unless new, evil parameter type can be

created

Page 14: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Pass-By-Reference Parameters Resembles normal parameters from

before Similar rules of scoping and lifetime apply Use parameter's value when used within

function For this, include & between type and

name Only where parameters listed, not in

arguments Spaces do not matter, so use what looks

best

void Mean(int a,int b, double& avg);double ReadIn(int & x, char & y);bool tooClose(double &sum, int tri);

Page 15: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Passing-By-Reference

Memory location NOT created for parameter Instead it is assigned memory location of

argument Updates argument's value with each

assignment All this works even though argument is out-

of-scope!

Page 16: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Passing-By-Reference Caveats For code to compile argument must be

variable Using argument's memory location, so this

required Since lack location to use, no literals or

expression Data types must match exactly for it to

work Need the memory location sizes to be

equal 0s & 1s must be interpreted in identical

manner Compiler will enforce rules strictly

Huge security holes can be opened by mistake

Page 17: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b);

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 18: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b);

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 19: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b);

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 20: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b);

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 21: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b); // Needs three arguments (still)

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 22: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b); // Needs three arguments (still)

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 23: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Will It Compile?

void doItNow(int i, double & j, char k);

int x, y, z;

double a, b, c;

...

doItNow(3, 4.5, 'a');

doItNow(y + 1, pow(4, b), '?');

doItNow(x + 4, b, 'x');

doItNow(x + y, a * b); // Needs three arguments (still)

doItNow(x, x, '.');

doItNow(z, c, '!');

Page 24: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Passing-By-Reference Usage

Limits how function used now & into future If more precision needed, cannot change

data types Needs variable for parameter, no matter

what If writing function that MUST return 2+

values Use return statement for one of the values Have other values "returned" with pass-by-

reference Much better to rewrite code to avoid

this Pass-by-reference often makes hard to

solve bugs

Page 25: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Passing-By-Reference Example

Page 26: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Passing-By-Reference Example

Page 27: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Tracing Example

int findMin(int & count) {int temp, min;cin >> min;count = 1;do { cin >> temp; if (temp < min) { min = temp; } count++; } while (temp != 100000);return min;

}

int main() {int small, num = 3small = findMin(num);cout << "In: " << num << ", min= " << small << endl;return 0;

}

Page 28: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

Your Turn

Get into your groups and try this assignment

Page 29: CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get

For Next Lecture

Read about arrays in Section 10.1 – 10.5 How can we use more than 1 value at a

time? What is meant by the term array? How are array used and why do we need all

these []? Weekly Assignment #7 out & due next

Tuesday

Also do not wait to start Program Assignment #2