Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?

Similar presentations


Presentation on theme: "PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?"— Presentation transcript:

1 PHY 107 – Programming For Science

2 Science Means Solving Problems  Physics – How does an atom work?

3 Science Means Solving Problems  Physics – How does an atom work?  Engineering – Will the bridge hold?

4 Science Means Solving Problems  Physics – How does an atom work?  Engineering – Will the bridge hold?  Biology – What is crawling up my leg?

5 Science Means Solving Problems

6 Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs using functions

7 Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs using functions  Already been doing this  Already been doing this, but should clarify process

8 Functions  Already written programs using functions  One function always present in all programs: main  cos, sin, floor also functions you've called before  main is a programmer-defined function  Pre-defined functions are similar, but part of C/C++  Programmers can define and use own functions cos radians cosine

9 Functions  Already written programs using functions  One function always present in all programs: main  cos, sin, floor also functions you've called before  main is a programmer-defined function  Pre-defined functions are similar, but part of C/C++  Programmers can define and use own functions Function Parameters Return Value

10 Why We Use Functions  Simplify code  Replace copies of code by placing in single location  Reuse commonly-used actions & calculations  With only limit that function can return a single value  Input & output performed in these functions  Will discuss ways to change parameters’ values

11 Function Basics

12 Return Type  Each function must declare a return type  Type of value returned by a function float floor(float x); double pow(double x, double t); int main();  May want nothing returned: use void as return type void printFormattedNumber(int x); void readAndPrintAverage();  Must return value from non- void function  If function is void, cannot return value

13 Function Declaration  When definition is after calling function…  Could be that function is later in file  Function in another file for use in many programs  Also important for built-in functions without bodies  Declare functions at start of the file  Often listed in header ( *.h ) files to enable reuse  #include "copy-and-paste" code from those files  Declaration lists vital information: function signature

14 Function Declarations

15

16 Function Definitions

17 Variable Scope  Variables create name for memory address  Name is not universal, but limited by the scope  Variable usable only in braces in which declared  For this copy of variable, scope defines its lifetime  Variable "dies" with end of scope in which declared  At start of scope, new copy created  Cannot use outside scope: error for bad variable within scope  Must have unique names within scope  If no overlap, can reuse names since older is unusable

18 Variable Scope int num = 3; void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main() { readNumber(num); readNumber(5); return 0; }

19 int num = 3; void readNumber(int len) { int num = 0; // This is name is not unique here! for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { readNumber(num); readNumber(5); return 0; } Variable Scope

20 void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; }

21 Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; }

22 Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; }

23 Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; }

24 Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c",&ch); num *= 10; num += ch – '0'; } printf("%d\n", num); } int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; }

25 Let's Trace void readNumber(int); int main(void) { int num = 3; readNumber(num); readNumber(5); return 0; } void readNumber(int len) { int num = 0; for (int i = 0; i < len; i++) { char ch; scanf("%c", &ch); num *= 10; num += ch – '0'; } printf("%d\n", num); }

26 For Next Lecture  Read more about functions in p. 179-180  Describe how value returned from a function  What happens to code after the return statement?  How do we pass values to a function?  What can a function do to those values? Tues.  Weekly Assignment #7 out & due next Tues.  Avoid the rush by start working on it now  Back to regular due dates, since no holidays for while


Download ppt "PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?"

Similar presentations


Ads by Google