Can you try replacing it with this version and removing the Topic tag from Velocity and see if it works as you would like?
\function topic.filter() [tag[Question]contains:topic<currentTiddler>]
<$list filter="[subfilter<topic.filter>first[]]" variable="_">
<aside class="kk-pdeck-topic-stat">
<span>Statistics</span>
<span>Exams: <$count filter="[topic.filter[]get[exam]enlist-input[]unique[]]"/></span>
<span>
<$button>
<$action-navigate $to={{{ [<currentTiddler>addprefix[Questions for ]] }}} $scroll="yes"/>
Questions:
</$button>
<$count filter="[topic.filter[]]"/>
</span>
</aside>
</$list>
Changes
I reformatted this and added the final closing tags (which TW allows to be optional, but which to my mind make the code much easier to read). These are all minor. The only substantive thing I changed was in the filter of the outer $list widget. Instead of [{!!title}tag[Topic]], I used [subfilter<topic.filter>first[]], where the function topic.filter was defined on the first line and used in two additional places throughout the template.
That topic.filter function finds the tiddlers tagged Question whose topic field includes the title of the current tiddler. For this outer $list guard, we want to see if there are any such questions. So we call that filter and use first[] to run the $list’s body only once. We also add a dummy variable so that it doesn’t override currentTiddler. Everything else remains as it was.
More Modern
However, your wiki is using a recent version of TW, which means you can use <% if %> / <% elseif %> / <% else %> / <% endif %>. That makes it a bit easier to write that outer wrapper:
\function topic.filter() [tag[Question]contains:topic<currentTiddler>]
<% if [subfilter<topic.filter>] %>
<aside class="kk-pdeck-topic-stat">
<span>Statistics</span>
<span>Exams: <$count filter="[topic.filter[]get[exam]enlist-input[]unique[]]"/></span>
<span>
<$button>
<$action-navigate $to={{{ [<currentTiddler>addprefix[Questions for ]] }}} $scroll="yes"/>
Questions:
</$button>
<$count filter="[topic.filter[]]"/>
</span>
</aside>
<% endif %>
Terminology
Part of the reason we were having a hard time understanding your request is that you were using “function” in a way very different from how TW thinks of them. Other terms were confusing too. Feel free to skip this section, but if you want to understand the issue, here’s my attempt at an explanation:
A function in TW accepts some number of parameters and returns a string. They are written in two ways:
1. Regular function definition
\function name(param1, param2, ...)
<!-- some filter here -->
\end
2. Single-line function definition
\function name(param1, param2, ...) <!-- some filter here -->
Your code sample includes a single-line function at the top. The rest of the code is not part of that function. But it does invoke that function – twice in your initial version, three times in my update.
So the title “How can I apply a filter to a function?” sounds nonsensical. A function is something you call to get back a value. Applying a filter to it makes no sense.
The important thing to note about the tiddler with that code is the tag $:/tags/ViewTemplate. That is what this block is, a template. (Again, it’s not a function, even though it happens to contain one.) What you’re really trying to do is to change the code that guards whether the remaining contents of this template will appear when your tiddler is rendered. This was a $list widget in your original and my first refactoring, an <% if %> block in my second version. What you’re calling “plonking” a button somewhere, we might call “conditionally rendering” it.
In any case, that’s why we got lost. Please let me know if this works, and if this explanation is clear!