Compute average value of a function (#581)