Programming and Graphics

(Kiana) #1

4.2 Static variables 93


{
greet1();
return 0;
}

void greet2();

//--- function greet1:

void greet1()
{
cout << "bye now" << " ";
greet2();
}

//--- function greet2:

void greet2()
{
cout << "come again" << endl;
}

Running this program prints on the screen:


bye now come again

The second function declarationvoid greet2()could have been stated before
the main program.


Problems


4.1.1.Investigate whether a function can call the main program.


4.1.2.Write a program that calls two functions, one function to print your
first name and another function to print your last name. The functions
should be declared before the main program and implemented after the
main program.


4.2 Static variables


Suppose that a variable is defined inside a function. When the function is exited,
the value of the variable evaporates. To preserve the variable, we declare it as
static using the qualifierstatic.

Free download pdf