EEB 334 - Chapter 4
EEB 334 - Chapter 4
EEB 334 - Chapter 4
Sheikh
Senior Lecturer
Department of Electrical Engineering
University of Botswana
Syntax:
Function_Name (Argument_list)
Examples
side = sqrt(area)
cout << “2.5 to the power 3.0 is ”
<< pow (2.5, 3.0);
• 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);
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;
}
• 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>
Some Predefined Function
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.
11
• A function Definition
#include <iostream.h>
int main()
{
double price, bill;
int number
return 0;
}
• The first form is used so that we can refer to the formal parameters in the
comment that accompanies the function prototype.
Procedural Abstraction
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
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.
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.
Overloading a Function Name
//Illustrates overloading the function name ave.
#include <iostream.h>
int main()
{
cout << “the average of 2.0, 2.5 and 3.0 is”
<< ave(2.0, 2.5, 3.0) << endl;
return 0
}
double ave(double n1, double n2)
{
return ((n1+n2)/2.0);
}
int main()
This variable named average_pea is local to the main part of the program
{ int max_count, min_count, pod_count;
double average_pea, yield;
cout << “Enter minimum and maximum number of peas in a
pod: ”;
cin >> min_count >> max_count;
cout << “Enter the number of pods: ”;
cin >> pod_count;
cout << “Enter the weight of an average pea (n ounces): ”;
cin >> average_pea;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout << “Min number of peas per pod = ” << min_count
<< endl
<< “Max number of peas per pod =” << max_count <<
endl
<< “Pod count = ” << pod_count << endl
<<”Average pea weight = ”
<< average_pea << “ ounces” << endl
<< “Estimated average yield = ” << yield << “ ounces”
<< endl;
return 0;
}
double est_total(int min_peas, int max_peas, int pod_count)
This variable named average_pea is local to the function est_total.
{ double average_pea;
Sample Dialogue
Enter minimum and maximum number of peas in a pod: 4 6
Enter the number of pods: 10
Enter the weight of a average pea (in ounces): 0.5
Min number of peas per pod = 4
Max number of peas per pod = 6
Pod count = 10
Average pea weight = 0.500 ounces
Estimated average yield = 25.000 ounces
A Global Named Constant
//Computes the area of a circle and the volume of a sphere.
/Uses the same radius for both calculations.
#include <iostream.h>
#include <math.h>
const double PI = 3.14159;
double area(double radius);
//Returns the radius of a circle with the specified radius.
double volume(double radius);
//Returns the volume of a sphere with the specified radius.
int main()
{
double radius_of_both, area_of_circle, volume_of_sphere;
cout << “Enter a radius to use for both a circle\n” << “and a
sphere (in inches): ”;
cin >> radius_of_both;
area_of_circle = area(radius_of_both);
volume_of_sphere = volume(radius_of_both);