Restucturing code into functions