Skip to content

G65: calling a macro and passing arguments

G65 calls a macro program and passes it values through the letters in the block itself, which are read inside the macro as numbered local variables. That is what turns a fixed subprogram into a parametric routine: instead of writing twenty versions of the same drilling job, you write one and call it with the numbers for each case. It differs from M98, which only jumps to the subprogram without passing anything, and from G66, which makes the call modal so it repeats at every positioning move.

For reference only: syntax changes with the control, the post-processor and the machine parameters. Check the official manual and run a simulation before cutting. Legal notice.

The brands and control names mentioned belong to their owners and are used only to identify the equipment. GetGCode is not affiliated with or endorsed by any manufacturer, ISO or ASME: this is our own content, not official documentation. Legal notice.

Family
Macros
Machine
Milling, Turning, Mill-turn
Risk
High
Example
G65 P9010 A10. B5.

Syntax by control

Fanuc (milling and turning)
G65 P9010 A10. B5. C2.5
P is the macro program number and the other letters are the arguments. Each letter is assigned a local variable inside the macro according to a fixed table in the control. It requires the user macro option to be installed.
Haas (milling and turning)
G65 P9010 A10. B5. C2.5
Same syntax. On some models macros are an option that has to be enabled, and on others they come standard. If the control does not have them, the block throws an alarm instead of running.
Fanuc G66 (modal call)
G66 P9010 A10. ; X20. Y10. ; X40. Y10. ; G67
G66 leaves the macro active: it runs at every positioning block until G67 cancels it. This is what you use to repeat an operation at many points without repeating the call.
Siemens 840D / 828D
MIRUTINA(10,5,2.5)
Siemens does not need G65: subprograms accept parameters declared with PROC and are called by name, with the arguments in parentheses and in the declared order. It is closer to a function in a regular programming language.

Address letters used with it

LetterWhat it setsUnit
PNumber of the macro program being called. It is not passed as an argument: it is the address of the call.program number
A B CThe first three arguments, read inside the macro as local variables 1, 2 and 3. They are the most used letters because they are the easiest to remember.numeric value
I J KArguments that, in the alternative addressing, let you pass repeated groups of three values, useful when the macro processes several positions.numeric value
D E F H M Q R S T U V W X Y ZThe rest of the letters accepted as arguments, each with its local variable assigned by the control. G, L, N, O and P cannot be used because they have another job in the block.numeric value
LNumber of repetitions of the call, same as in M98. It is not an argument: it is how many times the macro runs with those same values.integer
Local variablesInside the macro, arguments are read as variables in the local range. An argument that is not passed stays empty, and checking whether it is empty is the way to set default values.numeric variable

Worked example

Parametric bolt hole circle macro: you pass it the circle diameter, the number of holes, the angle of the first hole and the depth, and it calculates the positions and calls the drilling cycle. The main program uses it twice with different data. Local variable numbers are written generically here because their mapping to each letter is set by the control.

O0650 (PRINCIPAL - CIRCULO DE AGUJEROS)
( Main program. )
G21 G17 G40 G49 G80
( Safety block. )
G91 G28 Z0.
( Z to the reference point before the tool change. )
T7 M06
( Drill. )
G90 G54 G00 X0. Y0.
( Work offset and positioning at the center of the first circle. )
G43 H07 Z50. M03 S3000
( Length offset and spindle. )
M08
( Coolant. )
G65 P9010 A80. B6. C0. D-12.
( Macro call: Ø80 circle, six holes, the first at 0 degrees and 12 mm deep. Each letter travels to its local variable inside the macro. )
G00 X150. Y0.
( Moves to the center of the second circle, in another area of the part. )
G65 P9010 A50. B4. C45. D-8.
( Second call with different data: the same macro handles a different case without touching a line of code. )
G00 Z50. M09
( Retract and coolant off. )
G91 G28 Z0. M05
( Z to the reference point and spindle stop. )
G90 M30
( End of the main program. )
O9010 (MACRO CIRCULO DE AGUJEROS)
( The macro starts here. It lives as a separate program in the control's memory. )
(A=DIAMETRO B=N AGUJEROS C=ANG INICIAL D=PROF)
( A must-have comment in practice: without it, a year from now nobody remembers which letter is which. )
(guarda el centro actual en variables propias)
( The macro reads the current machine position to use it as the center of the circle, so the main program only has to position. )
(bucle WHILE sobre el numero de agujeros)
( Repeats as many times as argument B says, increasing the angle by 360 divided by B on each pass. )
(calcula X e Y con COS y SIN del angulo)
( User macro trig functions work in degrees, not radians: a detail that surprises anyone coming from another programming language. )
G81 Z[prof] R2. F200
( Drills at the calculated position, using the depth argument. )
G80
( Cancels the cycle when the loop ends. )
M99
( Returns to the calling program, right at the next block. )
Open the simulator to check the example

Practical keys to using G65 in the workshop

How the arguments get to the macro

Each letter you write in the G65 block is assigned a local variable inside the called macro, according to a fixed table in the control. The macro does not see letters: it sees numbers in variables, which is why the first thing a serious programmer should do is write a comment documenting the mapping. Arguments you do not pass stay empty, and a well-written macro checks for that and sets a default value instead of operating on nothing. Local variables belong to each call level, so a macro that calls another macro does not overwrite its values.

G65, M98 and G66: which one to use

M98 is the classic subprogram jump: it runs a fixed chunk of code and returns, passing no information. G65 is the jump with arguments, and it is what lets you write general routines. G66 is G65 made modal: the macro stays active and runs at every subsequent positioning move until G67 cancels it, which handles operations repeated at many points very well. In practice, M98 is for repeating blocks of code exactly as they are, and G65 for when what changes between uses is numeric values.

Why macros are sensitive code

A macro can calculate positions, move axes, change offsets and make decisions with conditionals, so a programming error does not give you a syntax alarm: it gives you a real move to a miscalculated position. On top of that, macros usually live in high-numbered programs, often protected, and the person who wrote them is no longer around. That is why they are dry run with machine lock on, commented obsessively, and their arguments limited with range checks that stop the program with an alarm message instead of machining.

Mistakes that cost you a part (or a tool)

  • Using a reserved block letter as an argument, such as G, L, N, O or P: the control reads it as part of the call, not as data.
  • Assuming an argument you did not pass equals zero: it stays empty, and operating on an empty variable gives unpredictable results depending on the control.
  • Confusing G65 with M98 and expecting the subprogram to receive values: M98 passes nothing and the macro will work with whatever was there before.
  • Keeping the mapping between letters and variables only in your head: the next person who touches the macro will not have it.
  • Leaving a modal G66 call without its G67: every later positioning move runs the macro where it should not.
  • Programming angles in radians in trig functions: user macros work in degrees.
  • Proving out a new macro with a part loaded instead of with machine lock on and checking the calculated positions on the screen.

Frequently asked questions

What is the difference between G65 and M98?

M98 jumps to a subprogram without passing it any information: the subprogram always does the same thing. G65 calls a macro and passes it values through the letters in the block, which are read inside as variables. If what you repeat is identical code, M98 is enough; if what changes is numbers, G65 saves you from having ten nearly identical subprograms.

Do I need an option to use G65?

Yes, user macros are a control option, although they come standard on many machines today. If your control does not have them, the G65 block gives an invalid code alarm. You can check by seeing whether the control lets you write and read variables on the macro screen.

Which letters can I use as arguments?

Nearly the whole alphabet except the ones the block needs for something else: G, L, N, O and P are out. Each accepted letter has its local variable assigned by the control, and the table is in the user macro manual. The practical approach is to use A, B, C, D and carry on from there, and document the mapping in the macro header.

How does a macro return a result to the main program?

Local variables die on return, so to hand something back you use common variables, which are shared between programs. That is the normal way to pass a probing measurement or a calculation to the main program. Be careful not to overwrite common variables used by another routine in the shop.

Can macro calls be nested?

Yes, up to the number of levels the control allows, which is usually four. Each level has its own set of local variables, so one macro can call another without the arguments overwriting each other. What is worth avoiding is overdoing it: a chain of four levels is very hard to debug on a machine screen.

Related codes

M98/M99M198Okuma M201-M220G31Mazak M96/M198/M199

Other codes in Macros

See the full list of G and M codes

Related apps