A variable (or object) is a place in memory where you can store
a value. Every variable has a type (for example, real), a name, and a value, and may have a storage class identifier (static or extern). All variables
must be declared before they can be used. By declaring a variable
ahead of time, the C compiler knows what kind of storage to allocate
for the value.
Global variables are variables that are defined outside of any
single function and are visible to all function(s) within a UDF source
file. Global variables can also be used by other functions outside
of the source file unless they are declared as static (see Static Variables). Global variables
are typically declared at the beginning of a file, after preprocessor
directives as in
#include "udf.h"
real volume; /* real variable named volume is declared globally */
DEFINE_ADJUST(compute_volume, domain)
{
/* code that computes volume of some zone */
volume = ....
}
Local variables are variables that are used in a single function.
They are created when the function is called, and are destroyed when
the function returns unless they are declared as static (see Static Variables). Local variables
are declared within the body of a function (inside the curly braces
"{" and "}"). In the example below, mu_lam and temp are local variables.
The value of these variables is not preserved after the function returns.
DEFINE_PROPERTY(cell_viscosity, cell, thread)
{
real mu_lam; /* local variable */
real temp = C_T(cell, thread); /* local variable */
if (temp > 288.)
mu_lam = 5.5e-3;
else if (temp > 286.)
mu_lam = 143.2135 - 0.49725 * temp;
else
mu_lam = 1.;
return mu_lam;
}