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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
C Language.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Pointers *, &, array similarities, functions, sizeof.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC Programming for Science Lecture 37 : Course Review.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
C Programming Lecture 8 Call by Reference Scope. Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must.
Functions + Overloading + Scope
User-Written Functions
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
Starting Out with Java: From Control Structures through Objects
Classes, Encapsulation, Methods and Constructors (Continued)
Lecture 18 Arrays and Pointer Arithmetic
Chapter 9: Value-Returning Functions
Presentation transcript:

PHY 107 – Programming For Science

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

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

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

Science Means Solving Problems

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

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

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

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

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

Function Basics

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

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

Function Declarations

Function Definitions

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

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; }

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

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; }

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; }

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; }

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; }

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; }

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); }

For Next Lecture  Read more about functions in p  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