PHY 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 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…
Variables
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
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
Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; }
Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; } One name -but- two memory locations
Variable Scope int findMin(int num) { int temp, min; scanf(“%d”, &min); num = 1; do { scanf(“%d”, &temp); if (temp < min) { min = temp; } num++; } while (temp != ); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; } 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 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 Copies value into memory location created for param Assignments affect memory location for parameter Unless new, evil parameter type can be created
Passing-By-Reference Memory location NOT created Memory location NOT created for parameter assigned memory location of argument Parameter 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!
Pass-By-Reference Basics
Passing-By-Reference Errors
Passing-By-Reference Warning Important to match type of argument & param C/C++ might show warning, but code will compile 0 s & 1 s misinterpreted since using wrong type When types differ, several outcomes possible Get incorrect results, but only for that variable Assignments change value of multiple variables Program crashes (but not necessarily immediately)
Will It Compile?
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
Passing-By-Reference Note
Tracing Example
Pass-By-Reference Basics
For Next Lecture Read about arrays in pages How can we use more than 1 value at a time? What is meant by the term array? How are arrays used and why do we need all these []? Weekly Assignment #7 out & due tomorrow Also do not wait to start Program Assignment #2