[ANSYS, Inc. Logo] return to home search
next up previous contents index

A.5 Variables

A variable (or object) is a place in memory where you can store a value. Every variable has a type (e.g., 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 Section  A.5.3). 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 Section  A.5.3). Local variables are declared within the body of a function (inside the curly braces {}). 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;
}




next up previous contents index Previous: A.4 Constants
Up: A. C Programming Basics
Next: A.5.1 Declaring Variables
Release 12.0 © ANSYS, Inc. 2009-01-14