Why can a mutation affect the function of protein?

Because a protein's function is dependent on is shape, and a mutation may change the composition (the sequence of amino acids) of a protein, which may in turn cause the shape of the molecule to change.