Functions and Decision Structures: CSSE 120—Rose Hulman Institute of Technology, Study notes of Software Engineering

Various aspects of functions and decision structures in python programming. It explains how functions can return values, the concept of multiple value return, and the difference between mutating parameters and returning new values. The document also discusses decision structures, specifically the if statement and its semantics, as well as more complex multi-way decisions using if-elif-else statements.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-9e1
koofers-user-9e1 🇺🇸

8 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FUNCTIONS THAT RETURN
RESULTS,
DECISION STRUCTURES
CSSE 120Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Functions and Decision Structures: CSSE 120—Rose Hulman Institute of Technology and more Study notes Software Engineering in PDF only on Docsity!

FUNCTIONS THAT RETURN

RESULTS,

DECISION STRUCTURES

CSSE 120—Rose Hulman Institute of Technology

Functions Can Return Values

 We've written functions that just do things  printFactorial(5)  printDistance(p1,p2)

 We've used functions that return values  abs(-1)  fn_root_1 = math.sqrt(bb – 4a*c)

 Define a function that returns a value  # _____________________ def square(x): return x * x

Returns the square of x

return statement

Pair Programming: Three Squares

 Download threeSquares.py from Angel

 Run the program to be sure it works

 Add a function, stats that takes a Rectangle, r , as a parameter and returns the area of r

 Change the program to display the area of each rectangle inside the shape

 Finally, change stats to return the area and perimeter (see figure at right)

 Upload to threeSquares dropbox on Angel

Example Display

Modifying Parameters

 How do functions send information back?

 Return statements  Mutating parameters

 Consider: def awardEC(score, extra): newScore = score + extra score = newScore

earned = 87 bonus = 4 awardEC(earned, bonus) print earned

Full Credit for Getting It Right

 def awardEC(score, extra): newScore = score + extra return newScore

earned = 87 bonus = 4 earned = awardEC(earned, bonus) print earned

Extra-Credit for Everyone!

def awardEC(scores, extras): for i in range(len(scores)): scores[i] = scores[i] + extras[i] earned = [87, 63, 94] bonuses = [3, 5, 0] awardEC(earned, bonuses) print earned

 Did the value of amounts change?

 No, it refers to the same list  The list's contents changed  Functions can change state of mutable objects

Simple Decisions

 The if statement

 if :

 Semantics: "if the condition is true, run the body, otherwise skip it"

 Simple conditions

  Some relational operators: Math < ≤ = ≥ > ≠ Python < <= == >= > !=

Class Exercise

 Define a function grade(score)

 Where score is an exam score  and result is "perfect", "passing", or "failing" based on the score

Having It Both Ways: if-else

 Syntax: if :

else:

 Semantics: "If the condition is true, execute the statements for true, otherwise execute the statements for false"

Individual Exercise on Using if-else

 Define a function countFailPass(scores) that

 takes a list of exam scores  returns two values:  the count of failing scores in the list (those less than 60), and  the count of passing scores in the list

 Examples:

countFailPass([57, 100, 34, 87, 74]) returns (2,3)countFailPass([59]) returns (1,0)countFailPass([]) returns (0,0)

 Upload to countFailPass dropbox on Angel

Multi-way Decisions

 Syntax: if :

elif :

elif :

… else:

reach here if condition1 is false reach here if condition1 is false AND condition2 is true reach here if BOTH condition1 AND condition2 are false

Cleaning the Bird Cage

 Advantages of if-elif-else vs. nesting

 Number of cases is clear  Each parallel case is at same level in code  Less error-prone

 Fix grade function to use if-elif-else statement instead of nesting