EXAMPLE IGOR PROGRAM WITH PROGRAMMING CONVENTIONS #pragma rtGlobals=1 // Use modern global access method. // Igor Programming Conventions for CHEM-5181 -- Mass Spec and Chromatography at CU-Boulder // Version 2, Ingrid 20-Aug-2009 // These are good practices from experience of many people. You'll have to develop your own eventually // but this is a good set to get started // CONSTANTS // Declare constants at the top of your procedure file only. Use ALL CAPS for constant names, so that // you can easily recognize them in your code. Include units in the name. CONSTANT AMU_kg = 1.6603e-27 // amu in kg CONSTANT UNITQ_C = 1.6e-19 // elementary charge in C CONSTANT NUMPTS = 100 // Number of points for my waves // Don't use all caps for anything else in your programs, so that you don't confuse constants with // variables, function names etc. Use mixed case, e.g. ForceX or TimeInQuad. // Global Variable -- use g as a prefix so that you can easily recognize these in your code // This variable remembers its value at all times -- use only if needed, as this can create painful bugs Variable/G gMyGlobal1 // Function should have a good name that describes what it does. (Note that these functions have bad // names for simplicity.) Function MyFunction(Input1, input2) // Everything in a function should be indented by 1 tab // Input variables need to be declared first Variable Input1, Input2 // Try to always comment what things mean, and to give them // meaningful names with units (e.g., vel_0_m_s, accel_m_s2). // You will come back to use your code after a while // and it will take a lot less time to relearn it if the code is READABLE // Quote from experienced programmer: "Write programs for ease of reading" NVAR gMyGlobal1 // You need this statement in order to be able to access the global variable // inside this function; without this you would get a compilation error Variable InternalVar1, InternalVar2 = 1 // These variables are internal ("local") to your function // Their values are lost when the function exits Variable iTime // Index for For loop, give them meaningful names, not just i, j... Make/O/N=(NUMPTS) wPosX_m, wPosY_m// Waves for X and Y position of an ion, with units. // Use the w prefix so that you can easily tell in // your code what is a wave gMyGlobal1 = 10 // Initialize the global variable // Use extra spaces in loop conditions and in calculations for ease of readability) For (iTime = 0; iTime < NUMPTS ; iTime += 1) // Everything inside of a loop (or If statement) should be indented by 1 tab, to make the code // easily readable wPosX_m[iTime + 1] = wPosX_m[iTime] + (InternalVar1 + 1) * InternalVar2 EndFor // Return statement. The function returns the position of the ion in the last time step // If you execute the function in the command line as Print MyFunction(2, 2) it should print 100 Return wPosX_m[NUMPTS - 1] End Function // Master function, exemplifies how the returned value from a function can be used // I.e. your function above returns a value and can be used in a formula, much like sin(2) or sqrt(2) // It should print 20 to the command line when executed Function MyMasterFunction() Print MyFunction(2,2) / 10 + MyFunction(1,1) * 0.1 End Function