(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Computer Software & Software Development H&K Chapter 1 Instructor – Gokcen Cilingir Cpt S 121 (June 20, 2011) Washington State University.
Software Development Method & C Language Elements H&K Chapter 1-2
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
(1 - 1) Computer Software & Software Development H&K Chapter 1 Instructor - Andrew S. O’Fallon CptS 121 (August 26, 2015) Washington State University.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
chap3 Chapter 3 Top-Down Design with Functions.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
1 CS161 Introduction to Computer Science Topic #9.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
User defined functions
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
About the Presentations
Programming Fundamentals Lecture #7 Functions
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Top-Down Design with Functions
Functions I Creating a programming with small logical units of code.
Top-Down Design with Functions
Presentation transcript:

(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 Problem Solving Example (1) Problem Statement: Write a program that computes your grade point average after completion of 3 courses. Inputs: – Grade point and number of credits for course 1 – Grade point and number of credits for course 2 – Grade point and number of credits for course 3 Outputs – Grade point average (GPA) Relevant formula: GPA = ((grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3) ) / total_num_credits

C. Hundhausen, A. O’Fallon 3 Problem Solving Example (2) Initial algorithm – Get the grade points earned from each class – Get the credit hours for each class – Compute the average of the grade points – Display the results Refined algorithm – Get the grade points earned from each class – Get the credit hours for each class – Compute the credits hours earned weighted_credits = (grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3); – Compute the average of the grade points gpa = weighted_credits / total_num_credits; – Display the results

C. Hundhausen, A. O’Fallon 4 Problem Solving Example (3) Implementation #include int main (void) { int num_credits1 = 0, num_credits2 = 0, num_credits3 = 0; double grade_point1 = 0.0, grade_point2 = 0.0, grade_point3 = 0.0, weighted_credits = 0.0, total_num_credits = 0.0, gpa = 0.0; /* Get the grade points and credits */ printf ("Please enter your grade point for computer science course 1: "); scanf ("%lf", &grade_point1); printf ("Please enter the number of credits possible for computer science course 1: "); scanf ("%d", &num_credits1); printf ("Please enter your grade point for calculus course 2: "); scanf ("%lf", &grade_point2); printf ("Please enter the number of credits possible for calculus course 2: "); scanf ("%d", &num_credits2); printf ("Please enter your grade point for physics course 3: "); scanf ("%lf", &grade_point3); printf ("Please enter the number of credits possible for physics course 3: "); scanf ("%d", &num_credits3);

C. Hundhausen, A. O’Fallon 5 Problem Solving Example (4) /* Compute credit hours earned */ weighted_credits = (grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3); total_num_credits = num_credits1 + num_credits2 + num_credits3; /* Compute gpa */ gpa = weighted_credits / total_num_credits; /* Display results */ printf ("GPA: %.2lf\n", gpa); return 0; }

C. Hundhausen, A. O’Fallon 6 Notes on Example It’s redundant! – We’re using the exact same sequence of commands (printf, scanf) to obtain the three grade points and credits Is there a better (less redundant, easier to read, more concise) way to approach this problem?

C. Hundhausen, A. O’Fallon 7 Top-Down Design (1) Allows us to manage the complexity of a problem by decomposing it into smaller, less complex subproblems A divide and conquer approach By solving each subproblem and combining solutions, we solve the overall problem We only need to solve each subproblem once, rather than having to “reinvent the wheel” each time

C. Hundhausen, A. O’Fallon 8 Top Down Design (2) Example: Compute and display the gpa Compute gpa Get the grade point for each class and credits Compute gpaDisplay the gpa get_grade_point() get_credits () compute_weighted_credits () compute_gpa () display_gpa ()

C. Hundhausen, A. O’Fallon 9 Functions (1) Enable us to implement top-down design Self-contained “mini-programs” that solve a problem General rule-of-thumb – 1 function = 1 task = 1 algorithm You already have some practical understanding of functions from your mathematical background – f(x) = x 2 - 4x + 4 – In C, we pass the value of x into a function called “f” and get a result back

C. Hundhausen, A. O’Fallon 10 Functions (2) May have “input arguments” (also called “input parameters”) – The inputs to the function May return results in two ways: – Function result: the return statement specifies this – “output arguments” (also called “output parameters”): arguments into which the function places values to be passed back to the caller (more advanced; we’ll look at these later)

C. Hundhausen, A. O’Fallon 11 Functions (3) Anatomy of a function prototype: void display_gpa (double gpa); Return value Function name Function argument list; should be “void” if function has no arguments

C. Hundhausen, A. O’Fallon 12 Functions (4) The GPA example revisited double get_grade_point (void); int get_credits (void); double compute_weighted_credits (double grade_point1, double grade_point2, double grade_point3, int num_credits1, int num_credits2, int num_credits3); double compute_gpa (double weighted_credits, int total_num_credits); void display_gpa (double gpa); int main (void) { int num_credits1 = 0, num_credits2 = 0, num_credits3 = 0; double grade_point1 = 0.0, grade_point2 = 0.0, grade_point3 = 0.0, weighted_credits = 0.0, total_num_credits = 0.0, gpa = 0.0; /* Get the grade points and credits */ grade_point1 = get_grade_point (); num_credits1 = get_credits (); grade_point2 = get_grade_point (); num_credits2 = get_credits (); grade_point3 = get_grade_point (); num_credits3 = get_credits (); /* Compute credit hours earned */ weighted_credits = compute_weighted_credits (grade_point1, grade_point2, grade_point3, num_credits1, num_credits2, num_credits3); /* Should we have a new function for the sum? If so what would it look like? total_num_credits = num_credits1 + num_credits2 + num_credits3; /* Compute gpa */ gpa = compute_gpa (weighted_credits, total_num_credits); /* Display results */ display_gpa (gpa); return 0; }

C. Hundhausen, A. O’Fallon 13 Functions (5) Definition of get_grade_point () /* Prompts the user for a grade point earned for a course */ double get_grade_point (void) { double grade_point = 0.0; printf ("Please enter your grade point for your course: "); scanf ("%lf", &grade_point); return grade_point; }

C. Hundhausen, A. O’Fallon 14 Functions (6) Definition of get_credits () /* Prompts the user for the number of credits for a course */ int get_credits (void) { int num_credits = 0; printf ("Please enter the number of credits possible for your course: "); scanf ("%d", &num_credits); return num_credits; }

C. Hundhausen, A. O’Fallon 15 Functions (7) Definition of compute_weighted_credits () double compute_weighted_credits (double grade_point1, double grade_point2, double grade_point3, int num_credits1, int num_credits2, int num_credits3) { double weighted_credits = 0.0; weighted_credits = (grade_point1 * num_credits1) + (grade_point2 * num_credits2) + (grade_point3 * num_credits3); return weighted_credits; }

C. Hundhausen, A. O’Fallon 16 Functions (8) Definition of compute_gpa () double compute_gpa (double weighted_credits, int total_num_credits) { double gpa = 0.0; gpa = weighted_credits / total_num_credits; return gpa; }

C. Hundhausen, A. O’Fallon 17 Functions (9) Definition of display_gpa () /* Outputs the calculated gpa to the screen */ void display_gpa (double gpa) { printf ("GPA: %.2lf\n", gpa); }

C. Hundhausen, A. O’Fallon 18 Functions (10) How Functions are compiled – Function prototypes tell compiler what functions are defined – When a function call is encountered within main, the compiler is already aware of it – After compilation of main function, each function is compiled Machine language statement inserted at end of each function that transfers control back to caller (in main) How functions are executed – When a function is called, memory for local variables is allocated – Memory is released upon completion of function execution (  local function variables do not “outlive” function)

C. Hundhausen, A. O’Fallon 19 Functions (11) Example 1: What happens when a function is called display_gpa (3.4); void display_gpa (double gpa) { printf ("GPA: %.2lf\n", gpa); } Actual argument Formal parameter

C. Hundhausen, A. O’Fallon 20 Functions (12) Why Use Functions: A Summary of Advantages – Break a large, complex solution into logical units Individual members of a programming team can work on each unit independently – Procedural abstraction The main function need not be aware of the details of how a function works—just what it does Thus, during high-level problem solving activities, we won’t get bogged down in detail We can defer the details until we’re ready to write the individual functions

C. Hundhausen, A. O’Fallon 21 Functions (13) Why Use Functions: A Summary of Advantages (cont.) – Reuse Recall our comment on the original version of the program to compute and display the gpa of classes – Redundant: Much code was duplicated Why re-write sections of code that have already been written and tested? Functions allow us to package up a solution into a bite- size chunk that can be reused over and over

C. Hundhausen, A. O’Fallon 22 Functions (14) Why Use Functions: A Summary of Advantages (cont.) – Testing Allows for more efficient testing and “bug” resolution Each function is tested as it is implemented

C. Hundhausen, A. O’Fallon 23 C Math Functions The C math library defines numerous useful mathematical functions This library is an excellent example of the power of functions – Commonly-used mathematical operations are packaged up in functions that can be re-used over and over

C. Hundhausen, A. O’Fallon 24 C Math Functions Some C Math Library Functions – int abs(int x) ( ) – double ceil(double) – double floor(double) – double cos(double) – double sin(double) – double tan(double) – double exp(double) – double fabs(double) – double log(double) – double log10(double) – double pow(double,double) – double sqrt(double)

C. Hundhausen, A. O’Fallon 25 Next Lecture… More examples of top-down design involving – Functions with and without input arguments – Functions with and without output values The use of test drivers to verify that functions work Common programming errors

C. Hundhausen, A. O’Fallon 26 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016

C. Hundhausen, A. O’Fallon 27 Collaborators Chris Hundhausen