0% found this document useful (0 votes)
5 views

3 - Procedural Abstraction and Function that return

The document discusses procedural abstraction and function usage in C++, emphasizing top-down design, predefined functions, and user-defined functions. It explains function calls, prototypes, local and global variables, and the concept of overloading function names. Additionally, it highlights the importance of treating functions as black boxes to simplify programming.

Uploaded by

kagorabasoma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3 - Procedural Abstraction and Function that return

The document discusses procedural abstraction and function usage in C++, emphasizing top-down design, predefined functions, and user-defined functions. It explains function calls, prototypes, local and global variables, and the concept of overloading function names. Additionally, it highlights the importance of treating functions as black boxes to simplify programming.

Uploaded by

kagorabasoma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

3 - Procedural Abstraction and

Function that return a value


• 3.1 Top-Down Design
• A good plan of attack for designing the algorithm is to
break down the task to be accomplished into a few
subtasks, decompose each of these subtasks into
smaller subtasks and so forth
• This method is called top-down design.
• C++ has facilities to include separate subparts inside of
a program.
• In C++ these subparts are called functions.

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)

• The expression sqrt (9.0) is called a function call.

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>

• Such lines are called include directives. The name


ending in .h is the name of a file known as a header file.
• If your program uses a predefined function from some
library, then it must contain a directive that names the
header file for that library, such as the following

#include <math.h>

6
• Some Predefined Function

Name Description Type of Arguments Type of value Library


returnedHeader

sqrt square root double double math.h


pow powers double double math.h
abs absolute value int int stdlib.h
for int
labs absolute value long long stdlib.h
for long
fabs absolute value double double math.h
for double
ceil ceiling (round up) double double math.h
floor floor double double math.h
(round down)

7
• 3.2.2 Type changing functions

• Recall that 9/2 is integer division, and evaluated to 4, not


4.5.
• If you want division to produce an answer of type double,
then at least one of the two numbers in the division must
be of type double.
• In C++ you can tell the computer to convert a value of
type int to a value of type double.
• E.g double (9)
• Using the type name double in this way is called type
casting

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>

double total_cost(int number_par, double price_par); //function prototype


//Computes the total cost, including 5% sales tax,
//on number_par items ata cost of price_par each.

int main()
{
double price, bill;
int number

cout << “Enter the number of items purchased: ”;


cin >> number;
cout << “Enter the price per item $”;
cin >> price;

bill = total_cost(number,, price); //FUNCTION CALL

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;
}

double total_cost(int number_par, double price_par)


{
const doubel TAX_RATE = 0.05; //5% sales tax
double subtotal

subtotal = price_par * number_par


return (subtotal + subtotal*TAX_RATE);
}

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

• The first form is used so that we can refer to the formal


parameters in the comment that accompanies the
function prototype.

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

• 3.5.1 Local Variables


• Variables that are declared within the body of a function
definition are said to be local to that function or to have
that function as their scope.
• Variables that are defined within the main body of the
program are said to be local to the main part of the
program or to have the main part of the program as their
scope.
• Such variables are said to be local variables
15
• 3.5.2 Global Constants and Global
variables
• It can happen that more than one function uses a named
constant. In this case, you place the declaration for
naming a constant at the beginning of your program,
outside of the body of all the functions and outside the
body of the main part of your program. The named
constant is then said to be a global named constant
and it can be used in any function definition that follows
the constant declaration
• It is possible to declare ordinary variables as global
variables, which are accessible to all function definitions
in the file.
16
• 3.6 Overloading a function name

• If you have two or more function definitions for the same


function name, that is called overloading.
• When you overload a function name, the function
definitions must have different numbers of formal
parameters or some formal parameters of different
types.
• When there is a function call, the compiler used the
function definition whose number of formal parameters
and types of formal parameters match the arguments in
the function call.
17
• Overloading a Function Name
//Illustrates overloading the function name ave.
#include <iostream.h>

double ave(double n1, double n2)


//returns the average of the two number n1 and n2

double ave(double n1, double n2, double n3)


//returns the average of the three number n1, n2 and n3

int main()
{
cout << “the average of 2.0, 2.5 and 3.0 is”
<< ave(2.0, 2.5, 3.0) << endl;

cout << “the average of 4.5 and 5.5 is”


<< ave(4.5, 5.5) << endl;

return 0
}
18
double ave(double n1, double n2)
{
return ((n1+n2)/2.0);
}

double ave(double n1, double n2, double n3)


{
return ((n1+n2+n3)/3.0);
}

• The use of the same function name to mean different


things is called polymorphism. Overloading is our first
example of polymorphism

19

You might also like