Over-the-air computation (AirComp) of a function (e.g., averaging) has recently emerged as an efficient multiple-access scheme for fast aggregation of distributed data at mobile devices (e.g., sensors) to a fusion center (FC) over wireless channels. To realize reliable AirComp in practice, it is crucial to adaptively control the devices' transmit power for coping with channel distortion to achieve the desired magnitude alignment of simultaneous signals. In this paper, we study the power control problem for AirComp over fading channels. Our objective is to minimize the computation error by jointly optimizing the transmit power at devices and a signal scaling factor (called denoising factor) at the FC, subject to individual average power constraints at devices. The problem is generally non-convex due to the coupling of the transmit power over devices and denoising factor at the FC. To tackle the challenge, we first consider the special case with static channels, for which we derive the optimal solution in closed form. The optimal power control exhibits a threshold-based structure. Specifically, for each device, if the product of the channel quality and power budget, called quality indicator, exceeds an optimized threshold, this device applies channel-inversion power control; otherwise, it performs full power transmission. Building on the results, we proceed to consider the general case with time-varying channels. To solve the more challenging non-convex power control problem, we use the Lagrangeduality method via exploiting its "time-sharing" property. The derived optimal power control exhibits a regularized channel inversion structure, where the regularization has the function of balancing the tradeoff between the signal-magnitude alignment and noise suppression. Moreover, for the special case with only one device being power limited, we show that the optimal power control for the powerlimited device has an interesting channel-inversion water-filling structure, while those for other devices (with sufficiently large power budgets) reduce to channel-inversion power control over all fading states.Numerical results show that the derived optimal power control remarkably reduces the computation error as compared with other heuristic designs.X. Cao and J. Xu are with the