The continuous-time random walk (CTRW) is a model of anomalous subdiffusion in which particles are immobilized for random times between successive jumps. A power-law distribution of the waiting times, psi(tau) similar to tau(-(1+alpha)), leads to subdiffusion (< x(2)> similar to t(alpha)) for 0 < alpha < 1. In closed systems, the long stagnation periods cause time averages to divert from the corresponding ensemble averages, which is a manifestation of weak ergodicity breaking. The time average of a general observable (U) over bar (t) = 1/t integral(t)(0) U[x(tau)]d tau is a functional of the path and is described by the well-known Feynman-Kac equation if the motion is Brownian. Here, we derive forward and backward fractional Feynman-Kac equations for functionals of CTRW in a binding potential. We use our equations to study two specific time averages: the fraction of time spent by a particle in half-box, and the time average of the particle's position in a harmonic field. In both cases, we obtain the probability density function of the time averages for t -> infinity and the first two moments. Our results show that both the occupation fraction and the time-averaged position are random variables even for long times, except for alpha = 1, when they are identical to their ensemble averages. Using our fractional Feynman-Kac equation, we also study the dynamics leading to weak ergodicity breaking, namely the convergence of the fluctuations to their asymptotic values.