There are two solutions:
* f(a,b)=a for all a,b, and
* f(a,b)=b for all a,b.
Clearly these functions meet the condition. We must show there are no others.
By setting a=b=c we get f(a,a)=a for all a. Next, for all a,b, the median of f(a,a), f(a,b), f(b,a) must equal the median of a,a,b, namely a, so for all a,b, one of f(a,b), f(b,a) is at most a and the other is at least a. Switching a,b, we also see that one of f(a,b), f(b,a) is at most b and the other is at least b. Therefore, we have
min{f(a,b),f(b,a)}≤min{a,b}(13)
and
max{f(a,b),f(b,a)}≥max{a,b}.(14)
Next, consider any three numbers a<b<c. The median of f(a,b), f(b,c), f(c,a) must equal b, so one of f(a,b), f(b,c) equals b (since f(c,a) must be either at most a or at least c). Similarly, considering f(a,c), f(c,b), f(b,a), we see that one of f(c,b), f(b,a) equals b. The numbers f(a,b), f(b,a) cannot both be b, by (13), and f(b,c), f(c,b) cannot both be b, by (13). We conclude that either
f(a,b)=f(c,b)=b(15)
or
f(b,c)=f(b,a)=b.(16)
In particular, for any a<b, choosing c>b arbitrarily, we see that one of f(a,b), f(b,a) must equal a. Likewise, for any b<c, choosing a<b arbitrarily, we see that one of f(b,c), f(c,b) must equal b.
Putting these two conclusions together, for any a=b, one of f(a,b), f(b,a) equals min{a,b} and the other equals max{a,b}. In other words, for a=b, {f(a,b),f(b,a)} and {a,b} are equal as sets. Call {a,b} a first-pair if f(a,b)=a and f(b,a)=b, and a second-pair if f(a,b)=b and f(b,a)=a.
Now again consider any three numbers a<b<c. If either {a,b} or {b,c} is a first-pair, then (15) cannot hold, so (16) holds, and {a,b} and {b,c} are both first-pairs. That is, {a,b} is a first-pair if and only if {b,c} is. Pick any other numbers a′ and c′ such that a′<b and c′>b. The same logic gives
{a′,b} is a first-pair⟺{b,c} is a first-pair⟺{a,b} is a first-pair⟺{b,c′} is a first-pair.
This shows that, given any p,q=b, {p,b} is a first-pair if and only if {q,b} is. Since b is arbitrary, we have for any distinct p,q,r,s that
{p,q} is a first-pair⟺{s,q}={q,s} is a first-pair⟺{r,s} is a first-pair.
That is, if some first-pair exists, then every pair is a first-pair, so f(a,b)=a for all a,b. Otherwise, every pair is a second-pair, so f(a,b)=b for all a,b. Thus, the only possibilities for the function f are the two solutions we initially identified.