Operators > Definition and Evaluation Operators > Example: Interleaving of Regular and Global Definitions
Example: Interleaving of Regular and Global Definitions
Special care should be applied when interleaving regular and global definitions, as results may not be what you expect.
When calculating a worksheet, PTC Mathcad makes two passes through the entire worksheet:
In the first pass, it evaluates global definitions only and ignores all regular definitions.
In the second pass, it evaluates regular definitions as well as regular and symbolic evaluations.
The following scenarios explain how the two calculation passes affect the result of interleaving regular and global definitions in a worksheet.
Defining a Global Variable in Terms of a Regular Variable
1. Use the regular definition operator to assign a constant value to variable u.
2. Use the global definition operator to assign an expression, that contains variable u, to f1.
Regular definitions are ignored during the first pass. The assignment of f1 fails because the value of u is not known when f1 is evaluated.
3. Evaluate variables u and f1.
Defining a Global Variable in Terms of Another Global Variable
1. Use the global definition operator to assign a constant value to variable v.
2. Use a global definition operator to assign an expression, that contains variable v, to f2.
The assignment of f2 passes because the value of v is known when f2 is evaluated during the first pass.
3. Evaluate variables v and f2.
4. Use a global definition operator to assign an expression, but this time include a global variable that is defined below.
Since global assignments are evaluated in top-down, left-to-right order, the assignment of f3 fails because the value of w is not known when f3 is evaluated during the first pass.
5. Evaluate variables w and f3.
Evaluating variable w passes because it is defined globally as a constant, but evaluating variable f3 fails because its value depends on the value of variable w which is not known when f3 is evaluated during the first pass.