Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.

Similar presentations


Presentation on theme: "PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get."— Presentation transcript:

1 PHY 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

5

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 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 != 100000); return min; } int main() { int small, num = 3 small = findMin(num); printf(“In: %d, min= %d\n”, num, small); return 0; }

9 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 != 100000); 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

10 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 != 100000); 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

11 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

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  Unless new, evil parameter type can be created

13 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!

14 Pass-By-Reference Basics

15

16 Passing-By-Reference Errors

17 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)

18 Will It Compile?

19

20

21

22

23

24

25 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

26 Passing-By-Reference Note

27

28 Tracing Example

29 Pass-By-Reference Basics

30 For Next Lecture  Read about arrays in pages 63 - 68  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


Download ppt "PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get."

Similar presentations


Ads by Google