Functions
Breaking Up Your Programs
Programs may be broken up into many
manageable functions.
Figure 6-1
Return Type
Name
Body
void main(void)
{
cout << Hello World\n;
}
13/06/1432
Calling a Function
Function Header
void displayMessage()
Function Call
displayMessage();
Program 6-1
#include <iostream.h>
//******************************************
// Definition of function displayMessage. *
// This function displays a greeting.
*
//******************************************
void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
}
void main(void)
{
cout << "Hello from main.\n";
displayMessage();
cout << "Back in function main again.\n";
}
4
13/06/1432
Program 6-2
//The function displayMessage is repeatedly called from a loop
#include <iostream.h>
//******************************************
// Definition of function displayMessage. *
// This function displays a greeting.
*
//******************************************
void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
}
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.
Hello from the function displayMessage.
Hello from the function displayMessage.
Back in function main again.
13/06/1432
Program 6-3
// This program has three functions: main, first, and second.
#include <iostream.h>
//*************************************
// Definition of function first.
*
// This function displays a message. *
//*************************************
void first(void)
{
cout << "I am now inside the function first.\n";
}
Program continues
//*************************************
// Definition of function second.
*
// This function displays a message. *
//*************************************
void second(void)
{
cout << "I am now inside the function second.\n";
}
void main(void)
{
cout << "I am
first();
//
second(); //
cout << "Back
}
13/06/1432
Program Output
I am starting in function main.
I am now inside the function first.
I am now inside the function second.
Back in function main again.
Program 6-4
// This program has three functions: main, deep, and deeper
#include <iostream.h>
//**************************************
// Definition of function deeper.
*
// This function displays a message.
*
//**************************************
void deeper(void)
{
cout << "I am now inside the function deeper.\n";
}
10
13/06/1432
Program continues
//**************************************
// Definition of function deep.
*
// This function displays a message.
*
//**************************************
void deep()
{
cout << "I am now inside the function deep.\n";
deeper(); // Call function deeper
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";
}
11
Program Output
I am starting in function main.
I am now inside the function deep.
I am now inside the function deeper.
Now I am back in deep.
Back in function main again.
12
13/06/1432
13
14
13/06/1432
Program 6-6
// This program demonstrates a function with a parameter.
#include <iostream.h>
// Function Prototype
void displayValue(int)
void main(void)
{
cout << "I am passing 5 to displayValue.\n";
displayValue(5);
//Call displayValue with argument 5
cout << "Now I am back in main.\n";
}
15
Program 6-6
//*********************************************
// Definition of function displayValue.
// It uses an integer parameter whose value is displayed.
//*********************************************
*
*
16
13/06/1432
Program Output
I am passing 5 to displayValue.
The value is 5
Now I am back in main.
17
Figure 6-5
displayValue(5);
18
13/06/1432
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";
displayValue(5); // Call displayValue with argument 5
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";
}
20
10
13/06/1432
Program Output
I am passing several values to displayValue.
The value is 5
The value is 10
The value is 2
The value is 16
Now I am back in main.
21
Program 6-8
// This program demonstrates a function with three parameters.
#include <iostream.h>
// Function prototype
void showSum(int, int, int);
void main(void)
{
int value1, value2, value3;
cout << "Enter three integers and I will display ";
cout << "their sum: ";
cin >> value1 >> value2 >> value3;
showSum(value1, value2, value3); // Call showSum with
// 3 arguments
}
22
11
13/06/1432
Program continues
//************************************************************
// Definition of function showSum.
*
// It uses three integer parameters. Their sum is displayed. *
//************************************************************
void showSum(int num1, int num2, int num3)
{
cout << (num1 + num2 + num3) << endl;
}
23
24
12
13/06/1432
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);
void main(void)
{
int whole = 12;
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;
}
25
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.
*
//**************************************************************
void changeThem(int i, float f)
{
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;
}
26
13
13/06/1432
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
and the value of f is changed to 27.5
Now back in main again, the value of whole is 12
and the value of real is 3.5
27
Figure 6-7
28
14
13/06/1432
29
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);
void child(int);
void senior(int);
void main(void)
{
int choice, months;
30
15
13/06/1432
Program continues
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2);
do
{
cout << "\n\t\tHealth Club Membership Menu\n\n";
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;
31
Program continues
if (choice != 4)
{
cout << "For how many months? ";
cin >> months;
}
switch (choice)
{
case 1:
adult(months);
break;
case 2:
child(months);
break;
case 3:
senior(months);
break;
case 4:
cout << "Thanks for using this ";
cout << "program.\n";
break;
32
16
13/06/1432
Program continues
default:
}
} while (choice != 4);
}
//***********************************************************
// Definition of function adult. Uses an integer parameter, mon.
*
// mon holds the number of months the membership should be
*
// calculated for. The cost of an adult membership for that many
*
// months is displayed.
*
//******************************************************************
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(int mon)
{
cout << "The total charges are $";
cout << (mon * 20.0) << endl;
}
34
17
13/06/1432
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.
*
//************************************************************
void senior(int mon)
{
cout << "The total charges are $";
cout << (mon * 30.0) << endl;
}
35
18
13/06/1432
37
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)
{
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);
}
38
19
13/06/1432
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.
*
//****************************************************************
void divide(float arg1, float arg2)
{
if (arg2 == 0.0)
{
cout << "Sorry, I cannot divide by zero.\n";
return;
}
cout << "The quotient is " << (arg1 / arg2) << endl;
}
39
40
20
13/06/1432
41
Figure 6-10
42
21
13/06/1432
Program 6-13
// This program uses a function that returns a value.
#include <iostream.h>
//Function prototype
int square(int);
void main(void)
{
int value, result;
cout << "Enter a number and I will square it: ";
cin >> value;
result = square(value);
cout << value << " squared is " << result << endl;
}
43
Program continues
//****************************************************
// Definition of function square.
*
// This function accepts an int argument and returns *
// the square of the argument as an int.
*
//****************************************************
int square(int number)
{
return number * number;
}
44
22
13/06/1432
45
Figure 6-11
result = square(value);
20
int square(int number)
{
return number * number;
}
46
23
13/06/1432
47
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;
cout << "Enter an integer and I will tell you ";
cout << "if it is even or odd: ";
cin >> val;
if (isEven(val))
cout << val << " is even.\n";
else
cout << val << " is odd.\n";
}
48
24
13/06/1432
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;
if (number % 2)
status = false; // The number is odd if there's a remainder.
else
status = true; // Otherwise, the number is even.
return status;
}
49
Program Output
Enter an integer and I will tell you if it is even or odd: 5
[Enter]
5 is odd.
50
25
13/06/1432
51
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
void main(void)
{
int num = 1;
cout << "In main, num is " << num << endl;
func();
cout << "Back in main, num is still " << num << endl;
}
52
26
13/06/1432
Program continues
//*********************************************************
// Definition of function func.
*
// It has a local variable, num, whose initial value, 20, *
// is displayed.
*
//*********************************************************
void func(void)
{
int num = 20;
cout << "In func, num is " << num << endl;
}
53
Program Output
In main, num is 1
In func, num is 20
Back in main, num is still 1
54
27
13/06/1432
Figure 6-8
Function main
num = 1
Function func
num = 20
55
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>
void func(void); // Function prototype
int num = 2; // Global variable
void main(void)
{
cout << "In main, num is " << num << endl;
func();
cout << "Back in main, num is " << num << endl;
}
56
28
13/06/1432
Program continues
//*****************************************************
// Definition of function func.
*
// func changes the value of the global variable num *
//*****************************************************
void func(void)
{
cout << "In func, num is " << num << endl;
num = 50;
cout << "But, it is now changed to " << num << endl;
}
57
Program Output
In main, num is 2
In func, num is 2
But, it is now changed to 50
Back in main, num is 50
58
29
13/06/1432
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
void main(void)
{
cout << "In main, num is not visible!\n";
func();
cout << "Back in main, num still isn't visible!\n";
}
59
Program continues
int num = 2; // Global variable
//*****************************************************
// Definition of function func
*
// func changes the value of the global variable num. *
//*****************************************************
void func(void)
{
cout << "In func, num is " << num << endl;
num = 50;
cout << "But, it is now changed to " << num << endl;
}
60
30
13/06/1432
Program Output
In main, num is not visible!
In func, num is 2
But, it is now changed to 50
Back in main, num still isn't visible!
61
62
31
13/06/1432
Program 6-19
// This program has an uninitialized global variable.
#include <iostream.h>
int globalNum; // Global variable. Automatically set to zero.
void main(void)
{
cout << "globalNum is " << globalNum << endl;
}
63
Program Output
globalNum is 0
64
32
13/06/1432
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)
{
displayStars();
cout << endl;
displayStars(5);
cout << endl;
displayStars(7, 3);
}
66
33
13/06/1432
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)
{
// Nested loop. The outer loop controls 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;
}
}
67
Program Output
**********
*****
*******
*******
*******
68
34