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

A.5.3 Static Variables

The static operator has different effects depending on whether it is applied to local or global variables. When a local variable is declared as static the variable is prevented from being destroyed when a function returns from a call. In other words, the value of the variable is preserved. When a global variable is declared as static the variable is "file global''. It can be used by any function within the source file in which it is declared, but is prevented from being used outside the file, even if is declared as external. Functions can also be declared as static. A static function is visible only to the source file in which it is defined.

figure   

static variables and functions can be declared only in compiled UDF source files.



Example - Static Global Variable


/*  mysource.c  /*

#include "udf.h"

static real abs_coeff = 1.0;  /* static global variable */
     /* used by both functions in this source file but is
        not visible to the outside  */

DEFINE_SOURCE(energy_source, c, t, dS, eqn)
{
 real source;    /* local variable
 int P1 = ....;  /* local variable  
                    value is not preserved when function returns */

 dS[eqn] = -16.* abs_coeff * SIGMA_SBC * pow(C_T(c,t),3.);
 source =-abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1));
 return source;
}

DEFINE_SOURCE(p1_source, c, t, dS, eqn)
{
 real source;
 int P1 = ...;
 dS[eqn] = -abs_coeff; 
 source = abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1));
 return source;
}


next up previous contents index Previous: A.5.2 External Variables
Up: A.5 Variables
Next: A.6 User-Defined Data Types
Release 12.0 © ANSYS, Inc. 2009-01-14