Managing Functions and DataGlobal and Static Variable in c++ languageManaging Functions and Data
This chapter covers some additional aspects of functions in C++. Recursion, a key concept in computer science is introduced.
Global Variables
All variables to this point have been local to functions or local to blocks within the bodies of conditional or iterative statements. Local variables have some very desirable properties:

- We can use the same variable name in different functions without any conflict. The compiler derives all of its information about a local variable used within a function from the declaration of that variable in that function.
#include <iostream>
#include <cmath>
void help_screen()
{
std::cout << "Add: Adds two numbers\n";
std::cout << " Example: a 2.5 8.0\n";
std::cout << "Subtract: Subtracts two numbers\n";
std::cout << " Example: s 10.5 8.0\n";
std::cout << "Print: Displays the result of the latest operation\n";
std::cout << " Example: p\n";
std::cout << "Help: Displays this help screen\n";
std::cout << " Example: h\n";
std::cout << "Quit: Exits the program\n";
std::cout << " Example: q\n";
}
/*
* menu
* Display a menu
* Accepts no parameters
* Returns the character entered by the user.
*/
char menu()
{
// Display a menu
std::cout << "=== A)dd S)ubtract P)rint H)elp Q)uit ===\n";
// Return the char entered by user
char ch;
std::cin >> ch;
return ch;
}
/*
* Global variables used by several functions
*/
double result = 0.0, arg1, arg2;
void get_input()
{
std::cin >> arg1 >> arg2;
}
void report()
{
std::cout << result << '\n';
}
void add()
{
result = arg1 + arg1;
}
void subtract()
{
result = arg1 - arg2;
}
int main()
{
bool done = false; // Initially not done
do {
switch (menu()) {
case 'A': // Addition
case 'a':
get_input();
add();
report();
break;
case 'S': // Subtraction
case 's':
get_input();
subtract();
report();
case 'P': // Print result
case 'p':
report();
break;
case 'H': // Display help screen
case 'h':
help_screen();
break;
case 'Q': // Quit the program
case 'q':
done = true;
break;
}
}
while (!done);
}
- We say the local declaration hides the global declaration in the scope of the local variable.
void uninitialized() {
int x; // Declare the variable
std::cout << x; // Then use it
}
Static Variables
C++ provides a way in which a variable local to a function can be retained in between calls shows how declaring a local variable static allows it to remain in the computer’s memory for the duration of the program’s execution
#include <iostream>
#include <iomanip>
/*
* count
* Keeps track of a count.
* Returns the current count.
*/
int count()
{
// cnt's value is retained between calls it
// is declared static
static int cnt = 0;
return ++cnt; // Increment and return current count
}
int main()
{
// Count to ten
for (int i = 0; i < 10; i++)
std::cout << count() << ' ';
std::cout << '\n';
}
//Output
1 2 3 4 5 6 7 8 9 10
By contrast, if you remove the word static from Listing 10.3 (counter.cpp), recompile it, and rerun it, it prints:
1 1 1 1 1 1 1 1 1 1
#include <iostream>
/*
* prompt requests an integer from the user and
* keeps track of the cumulative number of entries.
* Returns the value entered by the user.
*/
int prompt()
{
static int count = 0;
int value;
std::cout << "Please enter integer #" << ++count << ": ";
std::cin >> value;
return value;
}
int main()
{
int value1, value2, sum;
std::cout << "This program adds together two integers.\n";
value1 = prompt(); // Call the function
value2 = prompt(); // Call the function again
sum = value1 + value2;
std::cout << value1 << " + " << value2 << " = " << sum << '\n';
}
Overloaded Functions
In C++, a program can have multiple functions with the same name. The functions must be different somehow, or else the compiler would not know how to associate a call with a particular function definition.
void f() { /* ... */ }
void f(int x) { /* ... */ }
void f(double x) { /* ... */ }
void f(int x, double y) { /* ... */ }
void f(double x, int y) { /* ... */ }
Default Arguments
We can define functions that accept a varying number of parameters. Consider that specifies a function that counts down:
#include <iostream>
// Prints a count down from n to zero. The default
// starting value is 10.
void countdown(int n=10)
{
while (n > 0) // Count down from n to zero
std::cout << n-- << '\n';
}
int main()
{
countdown(5);
std::cout << "----------" << '\n';
countdown();
}
10
9
8
7
6
5
4
3
2
1
0
int sum_range(int n, int m=100) { // OK, default follows non-default
int sum = 0;
for (int val = n; val <= m; val++)
sum += val;
return val;
}
int sum_range(int n=0, int m=100)
{ // OK, both default
int sum = 0;
for (int val = n; val <= m; val++)
sum += val;
return val;
}