How to change Variable

How can i get a variable to change based off of a function? for instance.

var imageNumber = 1;

and when i click on the next button it kicks off roughly this function.

imageNumber ++;

effectively adding to the image number.

but it is confined only to that function.

the imageNumber outside of that function didnt change.

how can i get the next button to change the number outside of the function?