IN ORDER TO IMPLEMENT A SET OF RULES / TUTORIALOUTLET DOT COM IN ORDER TOIMPLEMENTASETOFRULES/TUTORIALOUTLET.COM | Page 14
16
and in the body of function main in lines 23 and 32. A function can
use global variables to share information with the calling function.
Example
In the source module in figure F1, Function main stores 20 into
variable len in line 23, and 8 into variable width in line 24 before it
calls function computeAreaPeri1 in line 26. In line 15, function
computeAreaPeri1 retrieves 20 from variable len and 8 from variable
width,
multiplies 20 by 8, and stores the result, 160.0 in variable area. In line
16, function computeAreaPeri1 retrieves 20 from variable len and 8
from variable width,
computes 2 * (20 + 8), and stores the result, 56.0 in global variable
peri. In line 27, function main retrieves 160.0 from global variable
area and prints it. In line 28, function main retrieves 56.0 from global
variable peri and prints it. A variable that is defined in the body of a
function is a local variable of that function. A local variable can only
be accessed in the body of the function in which it is defined.
Exercise F1*
Execute the following program and show its output for the input value
7: ©2011 Gilbert Ndjatou Page 171 #include <iostream>
using namespace std;
int gnum1, gnum2;
void funct(void)
{
int num = gnum1 + 10;
gnum1 + = num;
gnum2 = 2 * gnum1 + 5;
}
int main( )
{
gnum1 = 15;
funct( );
cout << endl << ―gnum1=‖ << gnum1 <<
―\tgnum2=‖ << gnum2;
cin >> gnum1;
funct( );