Wednesday, March 22, 2017

C++ Part 41

Review Part IV

Function Call
A function call is an expression consisting of the function name followed
by arguments enclosed in parentheses. If there is more than one argument,
the arguments are separated by commas. A function call is an expression
that can be used like any other expression of the type specified for the
value returned by the function.
SYNTAX
Function_Name(Argument_List)
where the Argument_List is a comma-separated list of arguments:
Argument_1, Argument_2, . . . , Argument_Last
EXAMPLES
side = sqrt(area);
cout << "2.5 to the power 3.0 is " << pow(2.5, 3.0);

A Function to Convert from int to double
The notation static_cast can be used as a predefined function
and will convert a value of some other type to a value of type double. For
example, static_cast(2) returns 2.0. This is called type casting.
(Type casting can be done with types other than double, but until later in
this book, we will do type casting only with the type double.)
SYNTAX
static_cast(Expression_of_Type_int)
EXAMPLE
int total_pot, number_of_winners;
double your_winnings;
. . .
your_winnings =
static_cast(total_pot)/number_of_winners;

Function Declaration
A function declaration tells you all you need to know to write a call to the
function. A function declaration is required to appear in your code prior to
a call to a function whose definition has not yet appeared. Function
declarations are normally placed before the main part of your program.
SYNTAX
Type_Returned Function_Name(Parameter_List);
Function_Declaration_Comment
where the Parameter_List is a comma-separated list of parameters:
Type_1 Formal_Parameter_1, Type_2 Formal_Parameter_2,...
..., Type_Last Formal_Parameter_Last
Example
double total_weight(int number, double weight_of_one);
//Returns the total weight of number items that
//each weigh weight_of_one.

A Function Is Like a Small Program
To understand functions, keep the following three points in mind:
■ A function definition is like a small program and calling the function is the
same thing as running this “small program.”
■ A function uses formal parameters, rather than cin, for input. The arguments to
the function are the input and they are plugged in for the formal parameters.
■ A function (of the kind discussed in this chapter) does not normally send any output
to the screen, but it does send a kind of “output” back to the program. The
function returns a value, which is like the “output” for the function. The function
uses a return statement instead of a cout statement for this “output.”

Procedural Abstraction
When applied to a function definition, the principle of procedural
abstraction means that your function should be written so that it can be
used like a black box. This means that the programmer who uses the
function should not need to look at the body of the function definition to
see how the function works. The function declaration and the accompanying
comment should be all the programmer needs to know in order to use
the function. To ensure that your function definitions have this important
property, you should strictly adhere to the following rules:
HOW TO WRITE A BLACK-BOX FUNCTION DEFINITION (THAT RETURNS
A VALUE)
■ The function declaration comment should tell the programmer any and all conditions
that are required of the arguments to the function and should describe
the value that is returned by the function when called with these arguments.
■ All variables used in the function body should be declared in the function body.
(The formal parameters do not need to be declared, because they are listed in
the function declaration.)

Make a Loop Body a Function Call
Whenever you have a loop nested within a loop, or any other complex
computation included in a loop body, make the loop body a function call.
This way you can separate the design of the loop body from the design of
the rest of the program. This divides your programming task into two
smaller subtasks.

No comments:

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two diļ¬€erent implementations of the same instru...