Page 251 - Computer_Science_F5
P. 251

Computer Science  Variable declaration        Scope of variables in C++
                                                     programming
           Since  variables represent a certain
           type of data, then, if you want to use a  Based on their scopes, variables in C++
           variable in a program, you are supposed  can be classified into three types. These
          FOR ONLINE READING ONLY
           to  define  its  name  and  data  type. That  are local, global, and static variables.
           system of expressing variables is known
           as variable declaration. It is a call to the  (a) Local variables
           compiler to prepare a memory size for     Local variables are declared within the
           a variable in relation  to its data type.   function block or inside the compound
           To declare a variable, use the following   statement.  The following Program
           syntax: datatype variableName.            Example 4.8 demonstrates variable
                                                     declaration in C++.
           Example of variable declaration:
            int x;                                          Program Example 4.8:

            double area;                              Variable declaration

            char t;
                                                               function ()

           x,  area  and  t  represent variable name,          {
           while int, double and char represent data                  int a;
           types.                                                     double b;
                                                               }
           Note:   If two or more variables are of
                   the same data type, they can be
                   grouped as datatype variable1,     Variables a and b are defined within the
                   variable2, ……., variablen;. For    block of function, and they can only
                   example, int x, y, z;              be accessed inside that block. Hence,
                                                      a and b represent local variables.

           Initialising variables                    (b) Global variables
           Initialising variables involves using the
           equal sign “=” to assign a value to the   Global variables are declared before the
           variable. The following syntax is used    main function  main(). Such variables
           to initialise variable: datatype variable   can be applied to any function within
                                                     the program. Global variables  are
           = value;.                                 also known as external variables.

                                                     The following Program Example  4.9
           An example of variable initialization is   demonstrates the use of global variables
           int i = 56;                               in C++ program.


                                                 242
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   242                                                     23/07/2024   12:33
   246   247   248   249   250   251   252   253   254   255   256