Professional Documents
Culture Documents
More on Functions
1
Functions with Parameters
Remember that a function performs some tasks when it
is called with a functio n call.
3
Functions with Parameters and Result
Example: function that computes and returns area of circle:
/*
Computes area of a circle with radius r.
*/
double compute_area(double r)
{
return (PI * pow(r,2));
}
The function requires one formal
parameter of type double which
4 represents the radius of a circle.
Functions with Parameters and Result
Example: function that computes and returns area of circle:
/*
Computes area of a circle with radius r.
*/
double compute_area(double r)
{
return (PI * pow(r,2));
}
/*
Computes area of a circle with radius r.
*/
double compute_area(double r)
{
return (PI * pow(r,2));
}
double compute_area(double r)
{
return (PI * pow(r,2));
}
8
Functions with Parameters and Result
double radius = 10.0;
double area = compute_area(radius);
double compute_area(double r)
{
return (PI * pow(r,2));
}
The expression is
evaluated to give the
value 314.159.
9
Functions with Parameters and Result
double =
radius radius
10.0;= 10.0;
area = area
double compute_area(radius);
= compute_area(radius);
The result
314.159 is
returned and double compute_area(double r)
replaces the {
function call return (PI * pow(r,2));
}
10
Functions with Parameters and Result
double radius = 10.0;
double area = compute_area(radius);
314.159 is
assigned to
area
double compute_area(double r)
{
return (PI * pow(r,2));
}
11
Functions with Parameters and Result
We can also use the result in a object cout.
Example:
12
Functions with Parameters and Result
double compute_area(double r)
{
return (PI * pow(r,2));
}
13
Functions with Parameters and Result
cout
printf("Area=%lf\n",
<< "Area = " << compute_area(10.0)
find_area(10.0)); << endl;
double compute_area(double r)
{
return (PI * pow(r,2));
}
The expression is
evaluated to give the
value 314.159.
14
Functions with Parameters and Result
The result
314.159 is double compute_area(double r)
returned and {
replaces the return (PI * pow(r,2));
function call }
15
Functions with Parameters and Result
16
Local Variables
A function may use variables just like the function main.
17
Local Variables
Example: The local
variable are a is
double compute_area(double r)
used to store
{
the result of the
double area;
computation
which is then
area = PI * pow(r, 2);
returned as the
result of the
return area;
function.
}
18
Functions with Result but no Parameters
We may have a function which returns a value but does
not have any parameters.
19
Functions with Result but no Parameters
Example:
/*
Reads the radius of a circle.
*/ No parameter.
double get_radius(void)
{
double radius; // local variable
return radius;
}
20
Functions with Result but no Parameters
When the function is called, we specify the variable
where we want to store the result.
Example:
21
Functions with Result but no Parameters
double radius = get_radius();
No value passed
double get_radius(void)
{
double radius;
return radius;
}
22
Functions with Result but no Parameters
double radius = get_radius();
return radius;
}
23
Functions with Result but no Parameters
double radius = get_radius();
The value is
assigned to
radius
double get_radius(void)
{
double radius;
return radius;
}
24
Complete Program
/*
Finds area of circle
*/
#include <iostream>
#include <cmath> // for pow function
using namespace std;
#define PI 3.14159
// function prototypes
double get_radius(void);
double compute_area(double r);
void display_area(double a);
25
Complete Program
int main(void)
{
double radius = get_radius();
display_area(area);
return 0;
}
26
Complete Program
/*
Reads the radius of a circle.
*/
double get_radius(void)
{
double radius; // local variable
return radius;
}
27
Complete Program
/*
Computes area of a circle with radius r.
*/
double compute_area(double r)
{
return (PI * pow(r,2));
}
/*
Displays area of a circle.
*/
void display_area(double a)
{
cout << "Area is " << a << endl;
}
28
Structure Charts with Data Flow
A structure chart can show data flow into and out of a
function
function1
function2
29
Structure Charts with Data Flow
main
30
Functions with Multiple Parameters
A function may have more than one parameter.
Example:
/*
Multiplies its first parameter by the power
of 10 specified by its second parameter.
*/
double scale(double x, int n)
{
double scale_factor; // local variable
// Function prototype
double get_number();
int get_scale();
double scale(double x, int n);
32
Complete Program
int main(void)
{
// Get values for num1 and num2
double num1 = get_number();
int num2 = get_scale();
return 0;
}
33
Complete Program
/*
Reads a real number.
*/
double get_number()
{
double num; // local variable
return num;
}
34
Complete Program
/*
Reads the scale number.
*/
int get_scale()
{
int num; // local variable
return num;
}
35
Complete Program
/*
Multiplies its first parameter by the power
of 10 specified by its second parameter.
*/
double scale(double x, int n)
{
double scale_factor; // local variable
return x * scale_factor;
}
36
Functions with Multiple Parameters
actual parameters
cout << "Result of call to function scale "
<< "is " << scale(num1, num2) << endl;
return x * scale_factor;
}
37
Rules for parameter list correspondence
▪ The number of actual parameters in the function call and
formal parameters in the function prototype must match.
38
General Format for Function Header
The general format for a function header is:
re turn-type functio n-name ( formal-parame te r lis t
)
The general format for a function prototype is:
re turn-type functio n-name ( formal-parame te r lis t
);
If the function does not return any result, write void for
the return type.
If the function does not require any parameters, write void
for the formal parameter list
No te : the parame te r name for the functio n pro totype is
optio nal.
39
The Function Data Area
Each time a function call is executed, an area of memory
is allocated for storing the function’s data.
40
The Function Data Area
After function main executes these statements:
double num1 = get_number();
int num2 = get_scale();
41
The Function Data Area
After function main executes these statements:
double num1 = get_number();
int num2 = get_scale();
2.5 num1
2 num2
42
The Function Data Area
? scale_factor
43
The Function Data Area
After function scale executes this statement:
scale_factor = pow(10, (double)n);
2 num2 2 n
100 scale_factor
44
The Function Data Area
After function scale executes this statement:
return x * scale_factor;
2.5 num1
2 num2
45
The Function Data Area
This statement in main function continues to run:
cout << "Result of call to function scale "
<< "is " << scale(num1, num2) << endl;
2.5 num1
2 num2
46
Scope
Scope determines the re g io n of the pro g ram in which an
object is visible – that is, the part of the program where
you can use the object’s name.
47
Scope
For a variable or parameter declared in a function, its
scope extends from its declaration to the end of the
function.
This is called local scope.
48
Scope
The concept of scope allows functions to have same names
for variables or parameters.
49
Scope Example
50
Scope Example
51
Function Documentation
The documentation for a function should include the
following:
▪ What the function does
▪ The condition that must be true befo re the function is
called – this is called the precondition
▪ The condition that must be true afte r the function
completes execution - this is called the
postcondition
53
Case Study 1
Problem:
A hardware company manufactures washer rims. To
estimate shipping costs, the company needs a program
that computes the weight of a specified quantity of
washer rims.
54
Case Study 1
Unde rs tand the pro ble m :
A washer rim looks like a donut.
d1
To compute its weight, you need the: d2
rim area
thickness
density of the material used
55
Case Study 1
Data requirements:
Problem constant
PI is 3.14159
Problem inputs
double hole_diameter; // diameter of hole
double edge_diameter; // diameter of edge
double thickness; // thickness of washer rim
double density; // density of material
double quantity; // number of washer rims made
Problem outputs
double weight; // weight of washer rims made
56
Case Study 1
Program variables
double rim_area; // area of washer rim
double unit_weight; // weight of one washer rim
Relevant Formulas
are a of a circle = π x radius 2
radius of a circle = diame te r / 2
rim are a = are a of oute r circle – are a of ho le
unit weig ht = rim are a x thickne s s x de ns ity
57
Case Study 1
Design the solution:
Algorithm:
1. Get the washer rim’s inner and outer diameters.
2. Get the washer rim’s thickness and material density .
3. Get the quantity of washer rims made.
4. Compute the washer rim area.
5. Compute the weight of one washer rim.
6. Compute the weight of the batch of washer rims.
7. Display the weight of the batch of washer rims.
58
Case Study 1
Refined algorithm:
1. Get the washer rim’s inner and outer diameters.
2. Get the washer rim’s thickness and material density .
3. Get the quantity of washer rims made.
4. Compute the washer rim area.
4.1 Compute outer circle radius and hole radius.
4.2 rim area is outer circle area – hole area.
5. Compute the weight of one washer rim.
6. Compute the weight of the batch of washer rims.
7. Display the weight of the batch of washer rims.
59
Case Study 1 - Complete Program
/*
* Computes the weight of a batch of washer rims
*/
#include <iostream>
#include <iomanip> // for setprecision()
#include <cmath> // for pow()
using namespace std;
#define PI 3.14159
// function prototypes
double compute_rim_area(double h_d, double e_d);
double compute_area(double r);
double compute_unit_weight(double r_a, double t, double d);
double compute_total_weight(double u_w, double q);
void print_weight(double t_w);
60
Case Study 1 - Complete Program
// main function
int main(void)
{
// input data
double hole_diameter; // diameter of hole
double edge_diameter; // diameter of edge
// output results
double weight; // weight of washer rims made
// program variables
double rim_area; // area of washer rim
double unit_weight; // weight of one washer rim
61
Case Study 1 - Complete Program
// Get inner and outer diameters
cout << "Inner diameter in cm: ";
cin >> hole_diameter;
return 0;
} // end of main function
63
Case Study 1 - Complete Program
// function definitions
/*
Finds area of a washer rim with
inner diameter h_d and outer diameter e_d
*/
double compute_rim_area(double h_d, double e_d)
{
// local variables
double hole_radius; // radius of hole
double edge_radius; // radius of edge
64
Case Study 1 - Complete Program
/*
Finds area of a circle with radius r.
*/
double compute_area(double r)
{
return (PI * pow(r, 2));
}
/*
Finds weight of one washer rim with rim area r_a
thickness t and density d.
*/
double compute_unit_weight(double r_a, double t, double d)
{
return r_a * t * d;
}
65
Case Study 1 - Complete Program
/*
Finds weight of batch of washer rims with
weight of one washer rim u_w and quantity q.
*/
double compute_total_weight(double u_w, double q)
{
return u_w * q;
}
/*
Displays weight of batch of washer rims
*/
void print_weight(double t_w)
{
cout << "\nThe expected weight of the batch is "
<< fixed << setprecision(2) << t_w << " grams.\n";
}
66