A macro isn't a good use case for functions, and your use case seems simple enough that you don't even need a function. Assuming your $passages looks something like this and you don't need a "deep" search:
<<set $passages = {
passage1: { visited: true, name: "Something strange" },
passage2: { visited: false, name: "Passage 2"},
passage3: { visited: false, name: "Another Passage", value: 42 },
passageZ: { visited: true, name: "Don't go here" },
}>>
Then you can find a random unvisited passage key like this:
<<set _passageKey = Object.keys($passages).filter(p => !$passages[p].visited).random()>>