Chapter 6 Functions: For Educational Purpose Only. Not To Be Circulated Without This Banner
Chapter 6 Functions: For Educational Purpose Only. Not To Be Circulated Without This Banner
Figure 6-1
Return Type
Name
Body
For Educational Purpose only. Not to be circulated without this banner.
void main(void)
{
cout << Hello World\n;
}
Calling a Function
Function Header
void displayMessage()
For Educational
Purpose
Function
Callonly. Not to be circulated without this banner.
displayMessage();
Program 6-1
#include <iostream.h>
For
//******************************************
// Definition of function displayMessage. *
// This function displays a greeting.
*
//******************************************
void displayMessage()
{
cout << "Hello
fromonly.
the function
Educational
Purpose
Not to bedisplayMessage.\n";
circulated without
}
this banner.
void main(void)
{
cout << "Hello from main.\n";
displayMessage();
cout << "Back in function main again.\n";
}
Program Output
Hello from main.
Hello from the function displayMessage.
Back in function main again.
For Educational Purpose only. Not to be circulated without this banner.
Figure 6-2
void displayMessage()
{
cout << Hello from the function displayMessage.\n;
}
void main(void)
For Educational
Purpose only. Not to be circulated without this banner.
{
cout << Hello from main.\n;
displayMessage();
cout << Back in function main again.\n;
}
Program 6-2
//The function displayMessage is repeatedly called from a loop
#include <iostream.h>
For
//******************************************
// Definition of function displayMessage. *
// This function displays a greeting.
*
//******************************************
void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
Educational
Purpose only. Not to be circulated without
}
this banner.
void main(void)
{
cout << "Hello from main.\n";
for (int count = 0; count < 5; count++)
displayMessage();
// Call displayMessage
cout << "Back in function main again.\n";
}
Program Output
Hello from main.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
For Educational Purpose only. Not to be circulated without this banner.
Hello from the function displayMessage.
Hello from the function displayMessage.
Back in function main again.
10
Program 6-3
// This program has three functions: main, first, and second.
#include <iostream.h>
//*************************************
// Definition of function first.
*
// This function displays a message. *
//*************************************
11
Program continues
//*************************************
// Definition of function second.
*
// This function displays a message. *
//*************************************
void second(void)
{
cout << "I am now inside the function second.\n";
}
12
Program Output
I am starting in function main.
I am now inside the function first.
I am now inside the function second.
For Educational
Purpose only.
to be circulated without this banner.
Back in function
mainNot
again.
13
Figure 6-3
void first(void)
{
cout << I am now inside function first.\n;
}
For
void second(void)
{
Educational
Purpose
only.
Not to
be circulated
without
cout <<
I am now
inside
function
second.\n;
}
this banner.
void main(void)
{
cout << I am starting in function main.\n;
first();
second();
cout << Back in function main again.\n
}
14
Program 6-4
// This program has three functions: main, deep, and deeper
#include <iostream.h>
//**************************************
// Definition of function deeper.
*
//Educational
This function
displays
a to
message.
* without this banner.
For
Purpose
only. Not
be circulated
//**************************************
void deeper(void)
{
cout << "I am now inside the function deeper.\n";
}
15
Program continues
//**************************************
// Definition of function deep.
*
// This function displays a message.
*
//**************************************
void deep()
{
cout << "I am now inside the function deep.\n";
deeper(); //Purpose
Call function
deeper
For Educational
only. Not
to be circulated without
cout << "Now I am back in deep.\n";
}
void main(void)
{
cout << "I am starting in function main.\n";
deep();
// Call function deep
cout << "Back in function main again.\n";
}
this banner.
16
Program Output
I am starting in function main.
I am now inside the function deep.
I am now inside the function deeper.
For Educational
Not to be circulated without this banner.
Now I amPurpose
back inonly.
deep.
Back in function main again.
17
Figure 6-4
void deep(void)
{
cout << I am now inside function deep.\n;
deeper();
cout << Now I am back in deep.\n;
}
For
void deeper(void)
{
Educational cout
Purpose
Notinside
to befunction
circulated
without this
<< Ionly.
am now
deeper.\n;
}
banner.
void main(void)
{
cout << I am starting in function main.\n;
deep();
cout << Back in function main again.\n
}
18
19
Program 6-5
// This program has three functions: main, first, and second.
#include <iostream.h>
// Function Prototypes
void first(void);
void second(void);
For
void main(void)
{
Educational
Purpose only. Not to be circulated
cout << "I am starting in function main.\n";
first();
// Call function first
second(); // Call function second
cout << Back in function main again.\n";
}
20
For
banner.
21
22
23
Program 6-6
// This program demonstrates a function with a parameter.
#include <iostream.h>
// Function Prototype
void displayValue(int)
For
void main(void)
{
Educational
Purpose only. Not to be circulated without this
cout << "I am passing 5 to displayValue.\n";
displayValue(5);
//Call displayValue with argument 5
cout << "Now I am back in main.\n";
}
banner.
24
Program 6-6
//*********************************************
// Definition of function displayValue.
// It uses an integer parameter whose value is displayed.
//*********************************************
*
*
25
Program Output
I am passing 5 to displayValue.
The value is 5
Now I am back in main.
For Educational Purpose only. Not to be circulated without this banner.
26
Figure 6-5
displayValue(5);
For Educational
Purpose only. Not num)
to be circulated without this banner.
void displayValue(int
{
cout << The value is << num << endl;
}
27
Program 6-7
// This program demonstrates a function with a parameter.
#include <iostream.h>
// Function Prototype
void displayValue(int);
void main(void)
{
cout << "I am passing several values to displayValue.\n";
For Educational
Purpose
to be circulated
this5 banner.
displayValue(5);
//only.
CallNot
displayValue
with without
argument
displayValue(10); // Call displayValue with argument 10
displayValue(2); // Call displayValue with argument 2
displayValue(16); // Call displayValue with argument 16
cout << "Now I am back in main.\n";
}
Program continues on next slide
28
void
displayValue(int
num)
For
Educational
Purpose only.
Not to be circulated without this banner.
{
cout
29
Program Output
I am passing several values to displayValue.
The value is 5
The value is 10
For Educational
The valuePurpose
is 2 only. Not to be circulated without this banner.
The value is 16
Now I am back in main.
30
Program 6-8
// This program demonstrates a function with three parameters.
#include <iostream.h>
// Function prototype
void showSum(int, int, int);
void main(void)
For
Educational Purpose only. Not to be circulated without this banner.
{
31
Program continues
//************************************************************
// Definition of function showSum.
*
// It uses three integer parameters. Their sum is displayed. *
//************************************************************
void showSum(int num1, int num2, int num3)
{
For Educational
Purpose
Not to<<beendl;
circulated
cout << (num1
+ num2only.
+ num3)
}
32
33
Figure 6-6
showSum(value1, value2, value3);
For Educational
Purpose
only. Notnum1,
to be circulated
without
this banner.
void
showSum(int
int num2,
int num3)
{
cout << num1 + num2 + num3 << endl;
}
34
35
Program 6-9
// This program demonstrates that changes to a function parameter
// have no effect on the original argument.
#include <iostream.h>
// Function Prototype
void changeThem(int, float);
For
void main(void)
{
int whole Purpose
= 12;
Educational
only. Not to be circulated without this banner.
float real = 3.5;
cout << "In main the value of whole is " << whole << endl;
cout << "and the value of real is " << real << endl;
changeThem(whole, real);
// Call changeThem with 2 arguments
cout << "Now back in main again, the value of ";
cout << "whole is " << whole << endl;
cout << "and the value of real is " << real << endl;
}
36
Program continues
//**************************************************************
// Definition of function changeThem.
*
// It uses i, an int parameter, and f, a float. The values of *
// i and f are changed and then displayed.
*
//**************************************************************
For
Educational
Purpose
void
changeThem(int
i, only.
floatNot
f) to be circulated without this banner.
{
i = 100;
f = 27.5;
cout << "In changeThem the value of i is changed to ";
cout << i << endl;
cout << "and the value of f is changed to " << f << endl;
}
37
Program Output
In main the value of whole is 12
and the value of real is 3.5
In changeThem the value of i is changed to 100
Forand
Educational
Purpose
Not to be
without this banner.
the value
of f isonly.
changed
to circulated
27.5
Now back in main again, the value of whole is 12
and the value of real is 3.5
38
Figure 6-7
39
40
Program 6-10
// This is a menu-driven program that makes a function call
// for each selection the user makes.
#include <iostream.h>
// Function Prototypes
void adult(int);
For Educational Purpose only.
void child(int);
void senior(int);
void main(void)
{
int choice, months;
41
Program continues
For
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
do
{
Educational
only. NotClub
to be
circulated Menu\n\n";
without this
cout <<Purpose
"\n\t\tHealth
Membership
cout << "1. Standard adult Membership\n";
cout << "2. child Membership\n";
cout << "3. senior Citizen Membership\n";
cout << "4. Quit the Program\n\n";
cout << "Enter your choice: ";
cin >> choice;
banner.
42
Program continues
For
if (choice != 4)
{
cout << "For how many months? ";
cin >> months;
}
switch (choice)
{
case 1:
adult(months);
Educational Purpose only. break;
Not to be circulated without this banner.
case 2:
child(months);
break;
case 3:
senior(months);
break;
case 4:
cout << "Thanks for using this ";
cout << "program.\n";
break;
43
Program continues
default:
cout << "The valid choices are 1-4. ";
cout << "Try again.\n";
}
} while (choice != 4);
}
//***********************************************************
// Definition of function adult. Uses an integer parameter, mon.
*
// mon holds the number of months the membership should be
*
For
Purpose
only.
to bemembership
circulatedfor
without
this banner.
//Educational
calculated for.
The cost
of Not
an adult
that many
*
// months is displayed.
*
//******************************************************************
44
Program continues
//********************************************************************
// Definition of function child. Uses an integer parameter, mon.
*
// mon holds the number of months the membership should be
*
// calculated for. The cost of a child membership for that many
*
// months is displayed.
*
//*************************************************************
void
child(intPurpose
mon)
For
Educational
only. Not to be circulated without this banner.
{
cout << "The total charges are $";
cout << (mon * 20.0) << endl;
}
45
Program continues
//*******************************************************************
// Definition of function senior. Uses an integer parameter, mon.
*
// mon holds the number of months the membership should be
*
// calculated for. The cost of a senior citizen membership for
*
// that many months is displayed.
*
46
For Educational
For how many Purpose
months 12 only. Not to be circulated without this banner.
The total charges are $480.00
Health Club Membership Menu
1. Standard adult Membership
2. child Membership
3. senior Citizen Membership
4. Quit the Program
Enter your choice: 4
Thanks for using this program.
47
48
Program 6-11
// This program demonstrates a function with a return
statement.
#include <iostream.h>
// Function prototype
void halfway(void);
For Educational Purpose only. Not to be circulated without this banner.
void main(void)
{
cout << "In main, calling halfway...\n";
halfway();
cout << "Now back in main.\n";
}
49
Program continues
//*********************************************************
// Definition of function halfway.
*
// This function has a return statement that forces it to *
// terminate before the last statement is executed.
*
//*********************************************************
For
Educational
Purpose only. Not to be circulated without this banner.
void
halfway(void)
{
cout << "In halfway now.\n";
return;
cout <<"Will you ever see this message?\n";
}
50
Program Output
In main, calling halfway...
In halfway now.
Now back in main.
For Educational Purpose only. Not to be circulated without this banner.
51
Program 6-12
// This program uses a function to perform division. If division
// by zero is detected, the function returns.
#include <iostream.h>
// Function prototype.
void divide(float, float);
void main(void)
{
For
Educational Purpose
float num1, num2;
cout << "Enter two numbers and I will divide the first\n";
cout << "number by the second number: ";
cin >> num1 >> num2;
divide(num1, num2);
}
52
Program continues
//****************************************************************
// Definition of function divide.
*
// Uses two parameters: arg1 and arg2. The function divides arg1 *
// by arg2 and shows the result. If arg2 is zero, however, the
*
// function returns.
*
//****************************************************************
Forvoid
Educational
Purpose
to be circulated without this banner.
divide(float
arg1, only.
floatNot
arg2)
{
if (arg2 == 0.0)
{
cout << "Sorry, I cannot divide by zero.\n";
return;
}
cout << "The quotient is " << (arg1 / arg2) << endl;
}
53
54
55
Figure 6-10
56
Program 6-13
// This program uses a function that returns a value.
#include <iostream.h>
//Function prototype
int square(int);
main(void)
Forvoid
Educational
Purpose only. Not to be circulated without this banner.
{
57
Program continues
//****************************************************
// Definition of function square.
*
// This function accepts an int argument and returns *
// the square of the argument as an int.
*
For Educational Purpose only. Not to be circulated without this banner.
//****************************************************
int square(int number)
{
return number * number;
}
58
59
Figure 6-11
result = square(value);
20
For Educational Purpose
only.square(int
Not to be circulated
without this banner.
int
number)
{
return number * number;
60
61
Program 6-15
// This program uses a function that returns true or false.
#include <iostream.h>
// Function prototype
bool isEven(int);
void main(void)
{
int val;
62
Program continues
//*********************************************************************
// Definition of function isEven. This function accepts an
*
// integer argument and tests it to be even or odd. The function
*
// returns true if the argument is even or false if the argument is
*
// odd.
*
// The return value is bool.
*
//*********************************************************************
bool isEven(int number)
{
bool status;
63
Program Output
Enter an integer and I will tell you if it is even or
odd: 5 [Enter]
5 is odd.
For Educational Purpose only. Not to be circulated without this banner.
64
65
Program 6-16
// This program shows that variables declared in a function
// are hidden from other functions.
#include <iostream.h>
void func(void);
// Function prototype
66
Program continues
//*********************************************************
// Definition of function func.
*
// It has a local variable, num, whose initial value, 20, *
// is displayed.
*
//*********************************************************
For
Educational
Purpose only. Not to be circulated without this banner.
void
func(void)
{
int num = 20;
cout << "In func, num is " << num << endl;
}
67
Program Output
In main, num is 1
In func, num is 20
Back in main, num is still 1
For Educational Purpose only. Not to be circulated without this banner.
68
Figure 6-8
Function main
This num variable is only
visible in function main.
For Educational Purpose only. Not to be circulated without this banner.
Function func
This num variable is only
num = 20
visible in function func.
num = 1
69
Program 6-17
// This program shows that a global variable is visible
// to all the functions that appear in a program after
// the variable's declaration.
#include <iostream.h>
For
void main(void)
{
cout << "In main, num is " << num << endl;
func();
cout << "Back in main, num is " << num << endl;
}
70
Program continues
//*****************************************************
// Definition of function func.
*
// func changes the value of the global variable num *
//*****************************************************
71
Program Output
In main, num is 2
In func, num is 2
But, it is now changed to 50
For Educational
Purpose
only.
Back in main,
num
is Not
50 to be circulated without this banner.
72
Program 6-18
// This program shows that a global variable is visible
// to all the functions that appear in a program after
// the variable's declaration.
#include <iostream.h>
void func(void); // Function prototype
73
Program continues
int num = 2; // Global variable
For
//*****************************************************
// Definition of function func
*
// func changes
theonly.
value
global variable
*
Educational
Purpose
Notoftothe
be circulated
withoutnum.
this banner.
//*****************************************************
void func(void)
{
cout << "In func, num is " << num << endl;
num = 50;
cout << "But, it is now changed to " << num << endl;
}
74
Program Output
In main, num is not visible!
In func, num is 2
But, it is now changed to 50
For Educational
Purpose
only.
Notisn't
to bevisible!
circulated without this banner.
Back in main,
num
still
75
76
Program 6-19
// This program has an uninitialized global variable.
#include <iostream.h>
int globalNum; // Global variable. Automatically set to zero.
void main(void)
For
{ Educational Purpose only. Not to be circulated without
cout << "globalNum is " << globalNum << endl;
}
this banner.
77
Program Output
globalNum is 0
78
79
Program 6-20
// This program shows that when a local variable has the
// same name as a global variable, the function only sees
// the local variable.
#include <iostream.h>
For
// Function prototypes
void texas();
Educational
Purpose only.
void arkansas();
80
Program continues
//******************************************
// Definition of function texas.
*
// The local variable cows is set to 100. *
//******************************************
void texas(void)
{
int cows = 100;
81
Program continues
//******************************************
// Definition of function arkansas.
*
// The local variable cows is set to 50.
*
//******************************************
82
Program Output
There are 10 cows in main.
There are 100 cows in texas.
There are 50 cows in arkansas.
For Educational
Purpose
only.are
Not10
to be
circulated without this banner.
Back in main,
there
cows.
83
Program 6-21
// This program has local and global variables. In the function
// ringUpSale, there is a local variable named tax. There is
// also a global variable with the same name.
#include <iostream.h>
void ringUpSale(void); // Function prototype
84
Program continues
For
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
do
{
ringUpSale();
cout << "Is there another item to be purchased? ";
cin >> again;
} while (again == 'y' || again == 'Y');
Educational
Purpose only. Not to be circulated without this banner.
tax = sale * taxRate;
total = sale + tax;
cout << "The tax for this sale is " << tax << endl;
cout << "The total is " << total << endl;
85
Program continues
//******************************************************************
// Definition of function ringUpSale.
*
// This function asks for the quantity and unit price of an item. *
// It then calculates and displays the sales tax and subtotal
*
// for those items.
*
//******************************************************************
void ringUpSale(void)
{ Educational Purpose only. Not to be circulated without this banner.
For
int qty;
float unitPrice, tax, thisSale, subTotal;
cout << "Quantity: ";
cin >> qty;
cout << "Unit price: ";
cin >> unitPrice;
thisSale = qty * unitPrice; // Get the total unit price
86
Program continues
sale += thisSale;
For
87
88
89
Program 6-22
// This program shows that local variables do not retain
// their values between function calls.
#include <iostream.h>
// Function prototype
Forvoid
Educational
Purpose only. Not to be circulated without this banner.
showLocal(void);
void main(void)
{
showLocal();
showLocal();
}
91
Program continues
//***********************************************************
// Definition of function showLocal.
*
// The initial value of localNum, which is 5, is displayed. *
// The value of localNum is then changed to 99 before the
*
// function returns.
*
//***********************************************************
92
Program Output
localNum is 5
localNum is 5
93
Program 6-23
//This program uses a static local variable.
#include <iostream.h>
void showStatic(void); // Function prototype
void main(void)
{
for (int count = 0; count < 5; count++)
showStatic();
}
//*************************************************************
// Definition of function showStatic.
*
// statNum is a static local variable. Its value is displayed *
// and then incremented just before the function returns.
*
//*************************************************************
void showStatic(void)
{
static int statNum;
cout << "statNum is " << statNum << endl;
statNum++;
}
94
Program Output
statNum is 0
statNum is 1
statNum is 2
For Educational
only. Not to be circulated without this banner.
statNum Purpose
is 3
statNum is 4
95
Program 6-24
// This program shows that a static local variable is only
// initialized once.
#include <iostream.h>
void showStatic(void); // Function prototype
void main(void)
For Educational
Purpose only. Not to be circulated without this banner.
{
96
Program continues
//***********************************************************
// Definition of function showStatic.
*
// statNum is a static local variable. Its value is displayed
// and then incremented just before the function returns.
*
//***********************************************************
97
Program Output
statNum is 5
statNum is 6
statNum is 7
For Educational
only. Not to be circulated without this banner.
statNum Purpose
is 8
statNum is 9
98
Program 6-25
// This program demonstrates default function arguments.
#include <iostream.h>
// Function prototype with default arguments
void displayStars(int = 10, int = 1);
void main(void)
For{Educational Purpose only. Not to be circulated without this banner.
displayStars();
cout << endl;
displayStars(5);
cout << endl;
displayStars(7, 3);
}
100
Program continues
//**************************************************************
// Definition of function displayStars.
*
// The default argument for cols is 10 and for rows is 1.
*
// This function displays a rectangle made of asterisks.
*
//**************************************************************
void displayStars(int cols, int rows)
{
For Educational
Purpose
only.
Notcontrols
to be circulated
// Nested loop.
The outer
loop
the rows
// and the inner loop controls the columns.
for (int down = 0; down < rows; down++)
{
for (int across = 0; across < cols; across++)
cout << "*";
cout << endl;
}
}
101
Program Output
**********
*****
For Educational Purpose only. Not to be circulated without this banner.
*******
*******
*******
102
103
104
Example:
void doubleNum(int &refVar)
{
refVar *= 2;
For}Educational Purpose only. Not to be circulated without this banner.
// The variable refVar is called
// a reference to an int
105
Program 6-26
// This program uses a reference variable as a function
// parameter.
#include <iostream.h>
// Function prototype. The parameter is a reference variable.
void doubleNum(int &);
void main(void)
For Educational Purpose only. Not to be circulated without this banner.
{
int value = 4;
cout << "In main, value is " << value << endl;
cout << "Now calling doubleNum..." << endl;
doubleNum(value);
cout << "Now back in main. value is " << value << endl;
}
106
Program continues
//************************************************************
// Definition of doubleNum.
*
// The parameter refVar is a reference variable. The value
*
// in refVar is doubled.
*
For
Educational Purpose only. Not to be circulated without this banner.
//************************************************************
void doubleNum (int &refVar)
{
refVar *= 2;
}
107
Program Output
In main, value is 4
Now calling doubleNum...
Now back in main. value is 8
For Educational Purpose only. Not to be circulated without this banner.
108
Program 6-27
// This program uses reference variables as function
// parameters.
#include <iostream.h>
// Function prototypes. Both functions use reference variables
// as parameters
void doubleNum(int &);
void getNum(int &);
109
Program continues
//*************************************************************
// Definition of getNum.
*
// The parameter userNum is a reference variable. The user is *
// asked to enter a number, which is stored in userNum.
*
//*************************************************************
void
getNum(int
&userNum)
For
Educational
Purpose
only. Not to be circulated without this banner.
{
110
Program continues
//************************************************************
// Definition of doubleNum.
*
// The parameter refVar is a reference variable. The value
*
// in refVar is doubled.
*
For
Educational Purpose only. Not to be circulated without this banner.
//************************************************************
void doubleNum (int &refVar)
{
refVar *= 2;
}
111
112
114
Program
6-28
#include <iostream.h>
// Function prototypes
int square(int);
float square(float);
void main(void)
{
int userInt;
For Educational
Purpose
float userFloat;
cout.precision(2);
cout << "Enter an integer and a floating-point value: ";
cin >> userInt >> userFloat;
cout << "Here are their squares: ";
cout << square(userInt) << " and " << square(userFloat);
}
115
Program continues
// Definition of overloaded function square.
// This function uses an int parameter, number. It returns the
// square of number as an int.
116
117
Program
6-29
// This program demonstrates overloaded functions to calculate
// the gross weekly pay of hourly-paid or salaried employees.
#include <iostream.h>
// Function prototypes
void getChoice(char &);
float calcWeeklyPay(int, float);
float calcWeeklyPay(float);
void main(void)
For{ Educational Purpose
char selection;
int worked;
float rate, yearly;
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << Do you want to calculate the weekly pay of\n";
cout << (H) an hourly-paid employee, or \n;
cout << (S) a salaried employee?\n;
118
Program continues
getChoice(selection);
switch (selection)
{
case H :
case h :
For
this banner.
119
Program continues
//***********************************************************
// Definition of function getChoice.
*
// The parameter letter is a reference to a char.
*
// This function asks the user for an H or an S and returns *
// the validated input.
*
//***********************************************************
120
Program continues
//***********************************************************
// Definition of overloaded function calcWeeklyPay.
*
// This function calculates the gross weekly pay of
*
// an hourly-paid employee. The parameter hours hold the
*
// hourly pay rate. The function returns the weekly salary. *
//***********************************************************
void calcWeekly(int
hours,
payRate)
For Educational
Purpose only.
Not tofloat
be circulated
without this banner.
{
return hours * payRate;
}
121
Program continues
//***********************************************************
// Definition of overloaded function calcWeeklyPay.
*
// This function calculates the gross weekly pay of
*
// a salaried employee. The parameter holds the employees *
// annual salary. The function returns the weekly salary.
*
//***********************************************************
void calcWeekly(float
For Educational
Purpose only. annSalary)
Not to be circulated without this banner.
{
return annSalary / 52.0;
}
122
123
124
Program 6-30
// This program shows how the exit function causes a program
// to stop executing.
#include <iostream.h>
#include <stdlib.h> // For exit
Forvoid
Educational
Purpose only.
Not to beprototype
circulated without this banner.
function(void);
// Function
void main(void)
{
function();
}
125
Program continues
//***********************************************************
// This function simply demonstrates that exit can be used *
// to terminate a program from a function other than main. *
//***********************************************************
126
Program Output
This program terminates with the exit function.
Bye!
For Educational Purpose only. Not to be circulated without this banner.
127
Program 6-31
// This program demonstrates the exit function.
#include <iostream.h>
#include <stdlib.h> // For exit
void main(void)
{
char response;
For Educational
Purpose only. Not to be circulated without this banner.
cout << "This program terminates with the exit function.\n";
cout << "Enter S to terminate with the EXIT_SUCCESS code\n";
cout << "or f to terminate with the EXIT_FAILURE code: ";
cin >> response;
128
Program continues
if (response == 'S')
{
cout << "Exiting with EXIT_SUCCESS.\n";
exit(EXIT_SUCCESS);
}
else
{
For Educational
Purpose only. Not to be circulated without this banner.
cout << "Exiting with EXIT_FAILURE.\n";
exit(EXIT_FAILURE);
}
}
129
Program Purpose
Output With
Other
Example
For Educational
only. Not
to be
circulatedInput
without this banner.
This program terminates with the exit function.
Enter S to terminate with the EXIT_SUCCESS code
or f to terminate with the EXIT_FAILURE code: f [Enter]
Exiting with EXIT_FAILURE.
130
For
banner.
132