Cells respond to the environment by regulating the expression of genes according to environmental signals. The relation between the input signal level and the expression of the gene is called the gene regulation function. It is of interest to understand the shape of a gene regulation function in terms of the environment in which it has evolved and the basic constraints of biological systems. Here we address this by presenting a cost-benefit theory for gene regulation functions that takes into account temporally varying inputs in the environment and stochastic noise in the biological components. We apply this theory to the well-studied lac operon of E. coli. The present theory explains the shape of this regulation function in terms of temporal variation of the input signals, and of minimizing the deleterious effect of cell-cell variability in regulatory protein levels. We also apply the theory to understand the evolutionary tradeoffs in setting the number of regulatory proteins and for selection of feed-forward loops in genetic circuits. The present cost-benefit theory can be used to understand the shape of other gene regulatory functions in terms of environment and noise constraints.