A question of function design?

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 27 15:17:35 PDT 2016


I'm using Derelict GLFW3 and I found the following GLFW3 code 
snippet in a demo.


float distance = 3.0;

extern(C) void key_callback(GLFWwindow* window, int key, int 
scancode, int action, int modifier) nothrow
{
     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
     {
         glfwSetWindowShouldClose(window, GL_TRUE);
     }
     if (key == GLFW_KEY_TAB && action == GLFW_PRESS)
     {
         distance = distance + .25;
     }
}


I'm looking at the call back function and all but one of the 
parameters are pass by values and there is no return type.  So 
they use this distance variable (is this called a global 
variable?) which I thought was considered bad programing and the 
classical example of a side-effect.  Isn't this particularly 
dangerous as programs get larger and larger.

So given the way this function was designed, how is a programmer 
supposed to convey information to the outside world?  Do I just 
suck it up?

I looked at D code projects using this function on git hub, but 
stuff there was either either like this or too complex.




More information about the Digitalmars-d-learn mailing list