next up previous contents index
Next: Program Execution Up: C++ Basics Previous: Pre-Processing   Contents   Index

Variables and Functions

Definitions of variables and functions play the same role in a figure that macro definitions do in a LATEX document: gathering and organizing information on which the figure depends. A variable is defined by supplying its type, name, and initial value. By far the most common data types in ePiX are double, P, and int. The name of a variable may consist (only) of letters (including the underscore character) and digits, and must begin with a letter:

my_var, my_var2,  _MY_var, __, aLongVariableName;  // valid
my-var,    2var, v@riable, $x, ${MY_VARIABLE}; // not valid
Variable names are case-sensitive. There are numerous conventions regarding the significance of capitalization. Generally, make names descriptive but not unwieldy, and avoid names that begin with an underscore (unless you know what you're doing).

A function accepts ``arguments'' and ``returns a value''. To define a function in C++, you must specify the return type, the name of the function, the types of the arguments, and the algorithm by which the value is computed from the inputs. The code block

double f(double x) 
{ 
  return sqrt(1-x*x); 
}
specifies the double-valued function $ f$ of one double variable defined by the formula $ f(x)=\sqrt{1-x^2}$. Several sample and source files (especially functions.cc) give more interesting examples. A function definition should be formatted as above for readability.


next up previous contents index
Next: Program Execution Up: C++ Basics Previous: Pre-Processing   Contents   Index
Andrew D. Hwang 2004-09-04