Download presentation
Presentation is loading. Please wait.
Published byClare Flynn Modified over 9 years ago
1
CSC 107 – Programming For Science
2
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
3
Variables Variable Variable name location to store data Only for humans; 0 x 7E8A2410 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…
4
Variables
6
Variable Scope Variables not universal, lifetime limited by scope usable 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
7
Variables not universal, lifetime limited by scope usable 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
8
Variables not universal, lifetime limited by scope usable 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
9
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; }
10
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
11
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
12
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 Copies value into memory location created for param Assignments affect memory location for parameter
13
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 Copies value into memory location created for param Assignments affect memory location for parameter Unless new, evil parameter type can be created
14
Pass-By-Reference Parameters
15
Passing-By-Reference Memory location NOT created Memory location NOT created for parameter assigned memory location of argument Instead it is assigned memory location of argument Updates argument's value Updates argument's value with each assignment All this works even though argument is out-of-scope!
16
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 0 s & 1 s must be interpreted in identical manner Compiler will enforce rules strictly Huge security holes can be opened by mistake
17
Will It Compile?
24
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 MUST 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
25
Passing-By-Reference Example
27
Tracing Example
28
Your Turn Get into your groups and try this assignment
29
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.