#pragma rtGlobals=1 // Use modern global access method. // Igor Programming Conventions for CHEM-5181 -- Mass Spec and Chromatography at CU-Boulder // Version 1, Jose 23-Oct-2007 // 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 //CONSTANT AMU = 1.6603e-27 // amu in kg CONSTANT UNITQ = 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 remember its value at all times -- use only if needed, as this can create painful bugs Variable/G gMyGlobal1 // Function 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. You wil 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 your global variable // inside this function, without this you would get a compilation error Variable InternalVar1, InternalVar2 = 1 // These variables are internal 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, wPosY // Waves for X and Y position of an ion. 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[iTime + 1] = wPosX[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[NUMPTS - 1] End Function // Master function, exemplifies how the return value of 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