Obviously, you squeeze the string to the fret, which shortens the string making a higher sound. But it occurred to me, that it's awfully convenient that you need one in the same place for all the strings. And if it's so awfully convenient, how come violinist put up with having to find the exact place to hold the strings, rather then getting frets?

Up to the point I started learning the violin, I sort of took it for granted, that it has to be the way it is. I mean, if Maiden plays on frets and it always looks the same, there's nothing interesting there, right? So here is the question we are going to answer:

The sound made by the strings depend on the composition, length and tautness of the string. It's easy to see that the compositions of the strings are quite different, and I bet you can't tighten them exactly the same way. So how come you still have to touch them at the same place to go up a half note? The answer is actually an interesting interplay between physics, some choices in music theory and human perception.

To understand what's happening, we'll need to go over some physics, but I promise to rest somewhere with a cat picture. Let's take a string that is tightened between two points. I'll skip solving the equations (you need to apply Newton's first law to a one-dimensional continuum), but you get from this

\begin{equation}
\rho dx \frac{\partial^2y}{\partial t^2} = F (\sin(\phi(x+dx)) - \sin(\phi(x)),
\end{equation}

to this

\begin{equation}
c^2 = \frac{F}{\rho}
\end{equation}

where $F$ is the force acting on the string (how taut it is) and $\rho$ is the so-called linear density of the string (more-or-less it's composition), which together define $c$, the speed of sound in our string.

When we pluck our string, so called standing waves form. Basically, we will hear sounds corresponding to wavelengths, that we can fit on the string. To be more precise, we need to fit only the half wave lengths. So on an empty string that is $L$ long, the loudest and smallest frequency (since the smallest frequency has the largest wavelength) we will hear is

\begin{equation}
f_0 = \frac{c}{2L}.
\end{equation}

We can also now see, that heavier (usually also thicker) strings will make lower sounds, while more tense string will make higher sounds. Of course, other, less loud, but larger frequencies also appear for the smaller wavelengths since, they can also fit on the string (this is what gives the different qualities to the same sounds on different instruments). Below you can see a graph of a 32.5 cm long violin A string, tuned to 440 Hz and showing the fundamental (440 Hz) frequency. You can pull the slider to make some overtones appear, with diminishing amplitudes.

The way our music is made up, relies on the ratios of frequencies. For example on octave is when one of the frequencies is double the other, while a perfect fifth has a ratio of 3:2. (It's interesting to note, that this means that the actual physical difference between a C and a D will be wildly different based on the octave they are in. Actually, most of human perception is non-linear like that). So if I have two strings (let's call them G and D), and I want to use a single fret at some $x$ cm-s from the top, the ratios of the new fundamental frequencies have to be the same. Since the different strings have different speed of sounds (different $c$-s), the ratio must not depend on $c$.

Let's take a look. The original frequency on the G string is

\begin{equation}
f_G = \frac{c_G}{2L},
\end{equation}

while if we shorten the string by $x$ with a fret, we get
\begin{equation}
f'_G = \frac{c_G}{2L - x},
\end{equation}

thus the ratio of the two sounds is

\begin{equation}
\frac{f'_G}{f_G} = \frac{c_G}{2(L-x)}\frac{2L}{c_G} = \frac{L}{L-x}
\end{equation}

which is as we can see independent of $c$, so we will get the same result for our other string as well. So now we know why frets work: because we differentiate musical sounds based on frequency ratios and the ratios within a string do not depend on neither the composition nor the tautness of the string, only the lengths.

As promised, a kitten with a guitar for a small rest (pic from here):

So why don't all string instruments have frets, if it works? Because it actually doesn't work, or not always. Pure harmonic intervals are frequency ratios of integer numbers, like 2:1, 3:2 or for the minor second 12:11. These are the ratios that we perceive as harmonious and pleasant. The problem is that if you take the Western music system, which splits the octave into 12 parts with the semitone system, it is absolutely impossible to fix the frequencies of all the sounds AND have harmonic interval all the time.

You can of course choose a subset of sounds with fixed frequencies, for which you can have harmonic intervals, but then you can only play in a certain key. So then how come guitars are freted and can play in different keys? Well, by cheating. They use the so called equal temperament scale, where only the octaves are nice clear harmonics, and everything else is sightly off. It's called equal temperament, because it keeps the octave, but splits the octaves into 12 equal parts (well, 12 equal ratios). Actually this is how pianos are tuned as well. And this is why violins are not fretted: if you do not fix the frequencies of the sounds, you can always adapt the sounds to get those pure harmonics.

This topic is quite complicated, and has a lot of historical evolution, as humans came up with more and more complicated music. Fortunately, there's a lot of material out there about this on wikipedia or here for example.