Thứ Ba, 21 tháng 1, 2014

Procedural Abstraction and Functions That Return a Value

Slide 4- 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Top Down Design

To write a program

Develop the algorithm that the program will use

Translate the algorithm into the programming
language

Top Down Design
(also called stepwise refinement)

Break the algorithm into subtasks

Break each subtask into smaller subtasks

Eventually the smaller subtasks are trivial to
implement in the programming language
Slide 4- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Benefits of Top Down Design

Subtasks, or functions in C++, make programs

Easier to understand

Easier to change

Easier to write

Easier to test

Easier to debug

Easier for teams to develop
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
4.2
Predefined Functions
Slide 4- 8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Predefined Functions

C++ comes with libraries of predefined
functions

Example: sqrt function

the_root = sqrt(9.0);

returns, or computes, the square root
of a number

The number, 9, is called the argument

the_root will contain 3.0
Slide 4- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 4.1
Function Calls

sqrt(9.0) is a function call

It invokes, or sets in action, the sqrt function

The argument (9), can also be a variable or an
expression

A function call can be used like any expression

bonus = sqrt(sales) / 10;

Cout << “The side of a square with area “ << area
<< “ is “
<< sqrt(area);
Slide 4- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Function Call Syntax

Function_name (Argument_List)

Argument_List is a comma separated list:
(Argument_1, Argument_2, … ,
Argument_Last)

Example:

side = sqrt(area);

cout << “2.5 to the power 3.0 is “
<< pow(2.5, 3.0);
Slide 4- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Function Libraries

Predefined functions are found in libraries

The library must be “included” in a program
to make the functions available

An include directive tells the compiler which
library header file to include.

To include the math library containing sqrt():
#include <cmath>

Newer standard libraries, such as cmath, also require
the directive
using namespace std;
Slide 4- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 4.2
Other Predefined Functions

abs(x) int value = abs(-8);

Returns absolute value of argument x

Return value is of type int

Argument is of type x

Found in the library cstdlib

fabs(x) double value = fabs(-8.0);

Returns the absolute value of argument x

Return value is of type double

Argument is of type double

Found in the library cmath
Slide 4- 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Type Casting

Recall the problem with integer division:
int total_candy = 9, number_of_people = 4;
double candy_per_person;
candy_per_person = total_candy / number_of_people;

candy_per_person = 2, not 2.25!

A Type Cast produces a value of one type
from another type

static_cast<double>(total_candy) produces a double
representing the integer value of total_candy
Slide 4- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Integer division occurs before type cast
Type Cast Example

int total_candy = 9, number_of_people = 4;
double candy_per_person;
candy_per_person = static_cast<double>(total_candy)
/ number_of_people;

candy_per_person now is 2.25!

This would also work:
candy_per_person = total_candy /
static_cast<double>( number_of_people);

This would not!
candy_per_person = static_cast<double>( total_candy /
number_of_people);

Không có nhận xét nào:

Đăng nhận xét