Q:

ArcGIS API - call function onClick()

I have managed to get my tiled maps working using the ArcGIS javascript API. However, I want to be able to switch layers (showing different years). I created a function that will do this:
require(["esri/map",
    "esri/layers/ArcGISTiledMapServiceLayer",
    "esri/geometry/Point",
    "esri/SpatialReference",
    "dojo/domReady!"],
    function (Map, Tiled, Point, SpatRef) {

... 

        function veranderTiled(jaar){
            map_Thema_2.removeAllLayers();
            tiled = new Tiled(
            "http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_"+jaar+"/MapServer");
            map_Thema_2.addLayer(tiled);
        }

        ... 

    });

I want to call this function when I, say, press a button at a random place on my page. Like this:
<input name="" type="button" onClick="veranderTiled(2015)" value="Ander jaar" />

The function itself works fine, but I cannot seem to call it from anywhere outside of the require(...) part. I am not sure how javascript handles such accessibility (mostly used to C++), but I do wonder what I should change in order to be able to call this function from outside this particular script.
Edit: as expected, the console returns that the function is not defined. Is there a way to point it to the right location (something like map.veranderTiled(2015)?)

A:

This is an issue of scope - as you surmised, the function is only defined within the scope of the require callback.
There are typically 2 ways to solve this problem.
The first is to give the button an ID and assign it the event handler within the require callback (assuming that either your scripts are at the end of body or you have required dojo/domReady! to wait for the body to load first):
require(..., function (...) {
    ...

    document.getElementById('veranderTiledButton').onclick = function () {
        veranderTiled(2015);
    };
});

(You could also use dojo/on to hook up the event, though it's not crucial in this case.)
The second way is to make the function globally available, which is generally discouraged since the global variable scope quickly becomes the wild west if your code does this sporadically:
var veranderTiled; // Declare outside to be globally available

require(..., function (...) {
    ...

    // Define inside to be able to use loaded modules
    veranderTiled = function (jaar) {
        ...
    };

    ...
});

