Hi there, I'm writing a program that should calculate the energy of a system. There are three subroutines written, which one is used depends on a random number that is generated. Basically I want the energy of the system to be updated each time one of these subroutines is carried out. What I'm having trouble with is figuring out a way that the variable "energy_box" is updated and printed on screen. Here is one of these subroutines:
So the general idea is that this "energy_box" is updated each time one of these subroutines runs. I have "return energy_box" in each of them. At the moment when I print to screen I simply get 0's. I know the way I'm attempting to do it at the moment is seriously flawed so any suggestions of a better way to do it would be much appreciated.
You could declare energy_box in main(), then send it to the functions as a reference. That way when you modify the value of it in the function, you are changing it in main as well, because it is actually the same variable. Technically the variable occupies the same memory address. That is the whole idea of references and pointers. References are safer than pointers, so are preferred.
Read up about it all in the articles & reference section on this page at the top left.