CS 31 Discussion, Week 4 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)

Slides:



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

Functions Prototypes, parameter passing, return values, activation frams.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Writing and Testing Programs Drivers and Stubs Supplement to text.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Functions:Passing Parameters by Value Programming.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Programming Functions: Passing Parameters by Reference.
Test 2 Part a You have 20 minutes Don’t forget to put your name on the test Closed book No computers Do your own work.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Modular Programming. Modular Programming (1/6) Modular programming  Goes hand-in-hand with stepwise refinement and incremental development  Makes the.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 3: User-Defined Functions I
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
What Actions Do We Have Part 1
Implementing Functions from a Detailed Design Quick Tips
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
Repetition Statements
User Defined Functions
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
CS150 Introduction to Computer Science 1
Starting Out with C++: From Control Structures through Objects
Course websites CS201 page link at my website: Lecture slides
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Introduction to Functions
Presentation transcript:

CS 31 Discussion, Week 4 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)

What is this? double bodyMassIndex(double height, double weight) { return weight/(height*height); }

Functions Review: Parts double bodyMassIndex(double height, double weight) { return weight/(height*height) * 703.0; } A function has a name, parameters, and a return type The return value of the function must be the same type as the return type

double bodyMassIndex(double height, double weight) { return weight/(height*height) * 703.0; } int main() { double myBMI = bodyMassIndex(5* , 150); cout << “My BMI: “ << myBMI; return 0; } Functions Review: Calling Functions are called from other code, which executes them and produces a value of the same type as the function

double bodyMassIndex(double height, double weight) { return weight/(height*height) * 703.0; } int main() { double myBMI = bodyMassIndex(5* , 150); cout << “My BMI: “ << myBMI; return 0; } Functions Review: Arguments When called, the values passed to the function are called arguments Each argument must match the type of its corresponding parameter

Functions Calling Functions double bmiMetric(double height, double weight) { return weight/(height*height); } double bmiEnglish(double height, double weight) { double weightKg = weight * ; double heightM = height * ; return bmiMetric(weightKg, heightM); } Note that they are defined separately, even though bmiEnglish() calls bmiMetric() What’s the advantage of having one call the other?

Functions and Modularity What’s wrong with having giant do-all functions? They’re not modular Modularity is the property of being reusable – Achieved by being self-contained and operating for a variety of inputs The many advantages to writing modular code: – Easier to reuse existing code – Easier to understand what code does – Easier to test A bug in a module can usually be constrained to just that module

No Modularity int main() { double height, weight; cout << “Enter your height(m), weight (kg): “ cin >> height >> weight; cout << “BMI : “ << weight/(height*height) << endl; cout << “Enter your height(in), weight (lbs): “ cin >> height >> weight; weight *= ; height *= ; cout << “BMI (from English): “; cout << weight/(height*height) * << endl; }

Poor Modularity void bmiMetric(double height, double weight) { cout << “BMI : “ << weight/(height*height); } void bmiEnglish(double height, double weight) { weight *= ; height *= ; cout << “BMI: “; cout << weight/(height*height) * 703.0; } The functions have redundant code They’re also not self-contained: they print to the screen, which assumes something about the caller (e.g. that they want stuff on the screen)

Good Modularity double bmiMetric(double height, double weight) { return weight/(height*height); } double bmiEnglish(double height, double weight) { double weightKg = weight * ; double heightM = height * ; return bmiMetric(weightKg, heightM) * 703.0; } They share the same basic calculation They’re appropriately named and can be used in any program that requires BMI calculation They return a value versus printing to the screen; the value could be used for anything, not just printing (storing to a file, comparisons, etc.)