Programming and Graphics

(Kiana) #1

102 Introduction to C++ Programming and Graphics


Problems


4.4.1.Split the prime-number code discussed in Section 4.3 into two files, one
containing the main program and the second containing thegetNfunction,
and write the necessary header file.


4.4.2.Split the combinatorial code discussed in Section 4.3 into two files, one
containing the main program and the second containing thecombinfunc-
tion, and write the necessary header file.


4.5 Functions with scalar arguments


An important concept in C++ is the distinction between global and local vari-
ables. The former are pervasive, whereas the latter are private to the individual
functions.


Global variables


Global variables are defined outside the main function and user-defined
functions. Because their memory addresses are communicated implicitly, their
values do not need to be passed explicitly through the function argument list
enclosed by parentheses.


The following code employs three global variables:

#include <iostream>
using namespace std;

void banana(); // function declaration

double a = 2.0;
double b = 3.0;
double c;

//---- main ---

int main()
{
banana ();
cout << a <<""<<b<<""<<c<<endl;
return 0;
}

//---- banana ---

void banana ()
{
Free download pdf