4.1 FAQ-286 How do I define global functions, global variables, and constants?
Last Update: 1/23/2025
Contents
Global Functions for use across Origin sessions
To define a global function for use across Origin sessions, view this page.
For further information on scope of a function, refer to this page.
Global Constants and Variables for use across Origin sessions
When Origin starts, a text file in the user file folder named CONST.CNF is read. If you have mathematical constants that you regularly use in your work, the CONST.CNF file is a good place to define them, as they will always be available when Origin runs
- If Origin is running, exit the program.
- Open the CONST.CNF file under User File Folder or start a fresh page in a text editor (e.g. Notepad) and enter the following information:
- Name CONST.CNF and save the file to your User Files Folder (UFF).
- To check your constant and variable values, run Origin, open the Script Window and type the name of the constant or variable, followed by "=" and press ENTER.
const e = 2.718281828459045 //Euler's number
Constants and Variables in the current Origin Session
To define constants and variables for the current session of Origin, you can simply define them using the Script Window:
- Open Script Window using the menu Window: Script Window
-
Type desired constant or variable assignment and press Enter at end of each line, such as:
const myconst = 1.234; double myvar = 12.34; int mynum = 127;
Then any subsequent calculations in the script window, or any other location where script can be executed such as Set Column Values dialog, these constants and variables can be accessed.
Constants and Variables specific to an Origin Project
To define constants and variables specific to a project, you can define these in the ProjectEvents.OGS file attached to the project:
- Open Code Builder from the View: Code Builder menu, and in the left panel, expand the Project tree and double-click to open ProjectEvents.ogs
- Add following codes in the AfterOpenDoc section, and save the file and save the project
- @global=1;
- Define your variables and constants
These definitions will then be local to the project.
The following mini tutorial shows how to add a user-defined variable at the Origin project level and then use that variable
|
Variables specific to a Worksheet
If you need to define some variables specific to a worksheet, and then update the results in other columns by changing this variable, follow these steps:
- Add a couple of columns to the current sheet, or to a new sheet, and place the variable names in 1st column and the values in the 2nd column. The names are purely for reference.
- When using Set Values dialog to compute values for other columns, you can then use the Before Formula Script panel to define local variables in your script to point to specific cells of your variables column, and then include them in your column formula in the upper panel. If the recalculate mode is set to Auto or Manual, then you can simply change the variable values and have the calculated values update. See the help file section on Set Values dialog for more information.
Constants in a Fitting Function
The Variables and Parameters page of the Fitting Function Builder wizard provides an edit box for defining constants. The constants can then be assigned values in the Constants tab of the page where the function body and the parameters are defined.
For more details, please refer to scope of variables.
Keywords:variable, define variable, constants, functions, global variable, @glob, @global, project variable, ORGSYS.CNF, Set Values, Before Formula Script, ProjectEvents.OGS, Script Window