3 - Procedural Abstraction and Function that return
3 - Procedural Abstraction and Function that return
1
• 3.2 Predefined Functions
• Sqrt (square root) is one of the predefined functions. The
value of the function starts out with is called its
argument. The value it computes is called the value
returned.
• The syntax for using the function is as follows:
the_root = sqrt (9.0)
2
• 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)
Examples
side = sqrt(area)
cout << “2.5 to the power 3.0 is ”
<< pow (2.5, 3.0);
3
• A function call
//computes the size of a dog house that can be purchased given the
//user’s buget
#include <iostream.h>
#include <math.h>
Int main()
{
const double COST_PER_SQ_FT =10;
double budget, area, length,_side;
cout << “Enter the amount budgeted for your dog house $”;
cin >> budget;
area = budget/COST_PER_SQ_FT;
length_side = sqrt(area);
4
•
cout.setf(ios : : fixed);
cout.setf(ios : : showpoint);
cout.precision (2);
cout << “For a price of $” << budget << endl
<< “I can build you a luxurious square dog house\n”
<< “that is ”<< length_side
<< “feet on each side. \n”;
return 0;
5
• Notice that there is another new element in the above
program
#include <math.h>
#include <math.h>
6
• Some Predefined Function
7
• 3.2.2 Type changing functions
8
• 3.3 Programmer – Defined Functions
• 3.3.1 Function Definition
• You can define your own functions, either in the same
file as the main part of your program or in a separate file
so that the function can be used by several different
programs.
• The description of the function is given in two parts that
are called the function prototype and function definition.
• The function prototype describes how the function is
called.
• C++ requires that either the complete function definition
or the function prototype appears in the code before the
function is called.
9
• A function Definition
#include <iostream.h>
int main()
{
double price, bill;
int number
10
cout.setf(ios : : fixed);
cout.setf(ios : : showpoint);
cout.precision (2);
cout << number << “ items at ”
<< “$” << price << “ each. \n”
• << “Final bill, including tax, is $” << bill
<< endl;
return 0;
}
11
• The function prototype tells you everything you need to
know in order to write a call to the function. It tell you the
name of the function, how many arguments the function
needs and the type of the arguments.
• To understand functions, keep the following three points
in mind
• A function 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 does not send an 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.
12
• 3.3.2 Alternate forms of function
prototypes
• The following two prototypes are equivalent:
double total_cost(int number_par, double price_par);
and
double total_cost(int , double );
13
• 3.4 Procedural Abstraction
• 3.4.1 The black Box Analogy
• A person who uses a program should not need to know
the details of how the program is coded.
• A function is like a small program and should be used in
a similar way.
• A programmer who uses a function in a program needs
to know what the function does, but should not need to
know how the function accomplishes its task.
• This is often referred to as treating the function like a
black box.
• Writing and using functions as if they were black boxes
is also called procedural abstraction.
14
• 3.5 Local Variables
int main()
{
cout << “the average of 2.0, 2.5 and 3.0 is”
<< ave(2.0, 2.5, 3.0) << endl;
return 0
}
18
double ave(double n1, double n2)
{
return ((n1+n2)/2.0);
}
19