What you're talking about is called callback and implemented using function pointers in C and C ++.
Since you mentioned overflow, let's take a real example directly from the freeglut source code. I will use glutIdleFunc instead of glutTimerFunc, because the code is simpler.
In oversaturation mode, the function callback function (that you supply glutIdleFunc) is a pointer to a function that takes no parameters and returns nothing. The typedef type is used to give this type of function a name:
typedef void (* FGCBIdle)( void );
Here, FGCBIdle (short for FreeGlut CallBack Idle) is defined as a pointer to a function that takes no parameters and whose return type is invalid. This is just a type definition that makes it easy to write an expression; it does not allocate any storage.
Freeglut has an SFG_State structure that contains various settings. Part of the definition of this structure:
struct tagSFG_State { FGCBIdle IdleCallback; };
The structure contains a variable of type FGCBIdle, which we set, is a different name for a specific function pointer. You can set the IdleCallback field to indicate the address of the function that the user provides with the glutIdleFunc function. A simplified definition of this function:
void glutIdleFunc( void (* callback)( void ) ) { fgState.IdleCallback = callback; }
Here fgState is an SFG_State variable. As you can see, glutIdleFunc takes one parameter, which is a pointer to a function that takes no parameters and returns nothing, this parameter name is a callback. Inside the IdleCallback function inside the global variable fgState is set to a callback provided by the user. When you call the glutIdleFunc function, you pass the name of your own function (e.g. glutIdleFunc (myIdle)), but what you really pass is the address of the function.
Later, inside the glutMainLoop-initiated large attenuation processing loop, you will find this code:
if( fgState.IdleCallback ) { fgState.IdleCallback( ); }
If the user has provided an idle callback, it is called in a loop. If you check out the function pointer lesson at the beginning of my post, you’ll better understand the syntax, but I hope the general concept makes more sense.