Declaring a Variable

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Declaring and using a Variable >

Declaring a Variable

The first step to using a variable in a CPL program is declaring the variable. Declaring a variable is simply a way of informing the computer that you would like to use a variable with a specific name, and that you will be storing a specific type of data in that variable.

To declare a variable

1.Determine which part of your application needs to use the variable.

i.Where you declare a variable is important to where you use it. This is known as the scope of the variable.

Global scope - any variable declared outside the main() function. Global variables can be accessed by any part of your application, although they cannot be accessed by other CPL's.

Local scope - any variable declared within the beginning and ending {} of a function. These variables can be accessed only by code within that function.

ii.For more information, see Variable Declaration and Scope in the CPL language reference.

2.In your programming file, enter a variable type, and then follow the type with the variable name you wish to use.

i.Variable types are described in About Variable Types. When naming your variables, be sure to conform to the following rules:

A variable can only contain letters, numbers and the underscore character.

A variable must begin with a letter.

A variable cannot contain any punctuation or spacing.

3.To declare multiple values of the same type on the same line, separate the variables by a comma.

i.The following example show different types of variable declaration.

int x;

int y;

float myFloat;

text myString;

char myChar;

short a, b;

 

Once you have declared a variable, you can assign a value to that variable. For more information, see Assigning a Variable. You can also declare a type of variable called an array that can store multiple values of the same type. For more information, see Creating and using an Array.