Upload
clare-flynn
View
212
Download
0
Embed Size (px)
Citation preview
LECTURE 18: PASSING BY REFERENCE:EVIL GENIUSES OF PARAMETERS
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 to do whatever you
want How to use parameters and what they are
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…
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
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
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
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
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
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;
}
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
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
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
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
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);
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!
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
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, '!');
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, '!');
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, '!');
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, '!');
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, '!');
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, '!');
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, '!');
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
Passing-By-Reference Example
Passing-By-Reference Example
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;
}
Your Turn
Get into your groups and try this assignment
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