Declaring Variables

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Writing a Function >

Declaring Variables

Once you declare your function, you can then declare any additional variables that you may need.

To declare a variable in a function

Declaring a variable in a function is just like declaring them in other locations: you determine what type of variable you need, and then you give it a name.

As with normal variable declaration, you can declare any number of variables. For more information, see About Variable Types.

Note: When you begin to write a function, you will likely not know what variables you will need. Don’t worry. Just add them in as you go. If you get to a piece of code that needs a variable, scroll back to the top of the function and insert the declaration. It is recommended to declare a variable as soon as you realize you need it, rather than wait until you are finished with the function. Doing so prevents “undeclared variable” errors.

The following example declares the integer variable z:

Multiply_X_by_Y(int x, int y)

{

   int z;
}

 

You can use the parameters as variables, as you would any other variable, to store and retrieve data. Just be careful you don’t overwrite a value may need later.

Once you have declared your variables, you can begin writing your function. For more information, see Writing a Function Body.