presentation-16.pptx - Classes Methods P U R E F U N C T I...

This preview shows page 1 - 7 out of 16 pages.

P U R E F U N C T I O N S, M O D I F I E R F U N C T I O N S, M E T H O D S, O BJ E C T O R I E N T E D P R O G R A M M I N G Classes & Methods
Agenda 21 Pure Functions, Modifier Functions Chpt. 13 Object Oriented Programming Chpt. 14 Methods Chpt. 14
Pure Functions Pure Functions – does not modify any of the objects passed to it as arguments & has no side effects by example, no side effects includes “getting user input”, “displaying a value”, “writing a file” by contrast a modifier would modify one or more of the objects it gets as arguments
Example - Time class In this example, the Time class corresponds to the way people record the time of day class Time : pass time = Time() time.hours = 11 time.minutes = 59 time.seconds = 30
addTime (as a Pure function) takes two parameters, but neither is modified def addTime(t1, t2): sumA = Time() sumA.hours = t1.hours + t2.hours sumA.minutes = t1.minutes + t2.minutes sumA.seconds = t1.seconds + t2.seconds if sumA.seconds >= 60: sumA.seconds = sumA.seconds - 60 sumA.minutes = sumA.minutes + 1 if sumA.minutes >= 60: sumA.minutes = sumA.minutes - 60 sumA.hours = sumA.hours + 1 return sumA
increment (as a Modifier)

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture