convert computed code to a function for calculating total sum