cs 31 discussion week 4 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS 31 Discussion, Week 4 PowerPoint Presentation
Download Presentation
CS 31 Discussion, Week 4

Loading in 2 Seconds...

play fullscreen
1 / 10

CS 31 Discussion, Week 4 - PowerPoint PPT Presentation


  • 56 Views
  • Uploaded on

CS 31 Discussion, Week 4. Faisal Alquaddoomi, faisal@cs.ucla.edu 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 31 Discussion, Week 4' - wynter-cannon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 31 discussion week 4

CS 31 Discussion, Week 4

Faisal Alquaddoomi, faisal@cs.ucla.edu

Office Hours: BH 2432,

MW 4:30-6:30pm,F 12:30-1:30pm (today)

what is this
What is this?

double bodyMassIndex(double height, double weight) {

return weight/(height*height);

}

functions review parts
Functions Review: Parts

doublebodyMassIndex(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
functions review calling
Functions Review: Calling

doublebodyMassIndex(double height, double weight) {

return weight/(height*height) * 703.0;

}

int main() {

doublemyBMI = bodyMassIndex(5*12 + 11, 150);

cout << “My BMI: “ << myBMI;

return 0;

}

  • Functions are calledfrom other code, which executes them and produces a valueof the same typeas the function
functions review arguments
Functions Review: Arguments

double bodyMassIndex(double height, double weight) {

return weight/(height*height) * 703.0;

}

int main() {

double myBMI = bodyMassIndex(5*12 + 11, 150);

cout << “My BMI: “ << myBMI;

return 0;

}

  • When called, the values passed to the function are called arguments
  • Each argument must match the type of its corresponding parameter
functions calling functions
Functions Calling Functions

double bmiMetric(double height, double weight) {

return weight/(height*height);

}

double bmiEnglish(double height, double weight) {

double weightKg = weight * 0.453592;

double heightM= height * 0.0254;

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
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
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 *= 0.453592;

height *= 0.0254;

cout << “BMI (from English): “;

cout << weight/(height*height) * 703.0<< endl;

}

poor modularity
Poor Modularity

void bmiMetric(double height, double weight) {

cout << “BMI : “ << weight/(height*height);

}

void bmiEnglish(double height, double weight) {

weight *= 0.453592;

height *= 0.0254;

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
Good Modularity

double bmiMetric(double height, double weight) {

return weight/(height*height);

}

double bmiEnglish(double height, double weight) {

double weightKg = weight * 0.453592;

double heightM= height * 0.0254;

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