Differentiability of piecewise functions