Calling Procedures

Slide 1:

  • A procedure is a named group of programming instructions that may have parameters and return values.
  • Procedures are referred to by different names, such as method or function , depending on the programing language.
  • Parameters are input values of a procedure. Arguments specify the values of the parameters when procedure is called.
  • A procedure call interrupts the sequencial execution of statements causing the program to execute the statements within the procedure before continuing. One the last statement in the procedure (or a return statement) has executed, flow or control is returned to the point immediately following where the procedure was called.

Slide 2:

  • When calling procedures, it's important to take notice to whether it returns data, or a block of statements .
  • If the procedure just returns a block of statements, you call the procedure by referring to the procedure name, and inputting the arguments.
  • If the procedure returns some sort of data like a boolean or value , then you will assign that value to a variable

Slide 3:

  • Assume the Temperature outside is Fahrenheit.
  • The procedure convertFahrenheit is intended to convert from Fahrenheit to Celsius.
  • Convert the following psuedo code to python
def convertFahrenheit():
    # code goes here
    return celsius

convertFahrenheit(outsideTemp)

Conversion:

def convertFarenheit(temp):
    celsius = temp - 32
    celsius = celsius * 5/9
    return celsius 
    
print("what is the temperature outside?")
outsideTemp = input()
outsideTemp = (convertFarenheit(int(outsideTemp)))
print(outsideTemp)
what is the temperature outside?
7.222222222222222

Developing Procedures

Slide 8:

Picking a descriptive name is important in case you revisit the code later on (separate words with capitals) There are 2 different types of procedures- ones that return a value and those that simply execute a block of statements Steps of developing procedure: picking a useful name, thinking of parameters (what data does the procedure need to know), making a flowchart or writing procedure in pseudocode, and actually developing the procedure.

Slide 9:

In this example, a teacher is writing a program that will replace the grade on a previous quiz if the new grade is better than the previous.

  • What would be a good name for this procedure?
  • What parameters do we need for this procedure?
  • Try writing this procedure out in python based on the given pseudocode
currentGrade <- currentPoints / 40
currentGrade <- current Grade * 100
if currentGrade > quizGrade
    quizGrade <- currentGrade
# the parameters that we would need: number of points, the quiz grade


def gradeCalculation (score):
    print("What was the quiz grade?")
    quizGrade = input()
    quizGrade = int(quizGrade)
    currentGrade = score / 40 
    currentGrade = currentGrade * 100
    if currentGrade > quizGrade:
        quizGrade = currentGrade
        print(quizGrade)
    else: 
        print(currentGrade)
        
print("What was the overall grade")

finalScore = input()
finalScore = (gradeCalculation(int(finalScore)))
print(finalScore)
What was the overall grade
What was the quiz grade?
167.5
None

Procedural Abstraction

  • One type of abstraction is procedural abstraction which provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it
  • This is very helpful in managing complexity in a program
  • Subdivision of a program into separate subprograms is called modulatiry
  • A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program reuse, which helps manage complexity
  • When a pre-written procedure is called, you don’t necessarily need to know the details of this, just what it does and how to call it
  • Simply, procedural abstraction is naming and calling a pre-written procedure
  • Making sure to include the right arguments so the procedure can do what its supposed to do is crucial

Complexity Example

One of the biggest advantages of procedural abstraction is managing complexity.

Think about the process of simplifying the code? What do you think the advantage of the code segment on the left is?

Code Segment 1 Code Segment 2
ROTATE_LEFT() detourLeft()

MOVE_FORWARD()|turnCorner()| ROTATE_RIGHT |MOVE_FORWARD()| MOVE_FORWARD()|MOVE_FORWARD()| MOVE_FORWARD() ROTATE_RIGHT() MOVE_FORWARD() ROTATE_LEFT() MOVE_FORWARD() ROTATE_LEFT() MOVE_FORWARD() MOVE_FORWARD MOVE_FORWARD()

Hacks

  • Write a python procedure about something which can help you in school, for example the teacher’s function we spoke about earlier.
  • Points will be awarded based on creativity and functionality
  • 0.1 points will be deducted for late submissions
  • Submit the notes with all blanks filled in (scored out of 0.5 points) and the python procedure (scored out of 0.5 points) by Monday 12/12 at 11:59 PM.
print("what day is it? type the first letter, or 'th' for thursday")
day = input ()
if day == "m":
    print("its monday")
elif day == "t": 
    print("its tuesday")
elif day == "w":
    print("its wednesday")
elif day == "th":
    print("its thursday")
elif day == "f": 
    print("its friday")
elif day == "s":
    print("its the weekend! (what do you need the schedule for?)")


print("what time is it?") #this only works if you write the time as a decimal because im not smart enough to do actual time
time = float(input()) 
print("the time is: ", time)

if time < 8.00:
    time = time + 12


def schedule(time):
    if day == "w": #wednesday 
        if time < 9.55: 
            print("school has not started yet!")
        elif 9.55 <= time <= 10.53:
            print("its 1st period")
        elif 10.58 <= time <= 11.56:
            print("its 2nd period")
        elif 11.56 < time <= 12.06:
            print("its break")
        elif 12.11 <= time <= 13.09: 
            print("its 3rd period")
        elif 13.09 < time <= 13.39:
            print("its lunch")
        elif 13.44 <= time <= 14.42: 
            print("its 4th period")
        elif 14.47 <= time <= 15.45: 
            print("its 5th period")
        elif time > 15.45:
            print("school is over :)")
        else:
            print("its passing period") 
    elif day == "f": #friday
        if time < 8.35: 
            print("school has not started yet!")
        elif 8.35 <= time <= 9.49:
            print("its 1st period")
        elif 9.54 <= time <= 11.08:
            print("its 2nd period")
        elif 11.08 < time <= 11.18:
            print("its break")
        elif 11.23 <= time <= 12.37: 
            print("its 3rd period")
        elif 12.37 < time <= 13.07:
            print("its lunch")
        elif 13.12 <= time <= 14.26: 
            print("its 4th period")
        elif 14.31 <= time <= 15.45: 
            print("its 5th period")
        elif time > 15.45:
            print("school is over :)")
        else:
            print("its passing period") 
    else:
        if time < 8.35: 
            print("school has not started yet!")
        elif 8.35 <= time <= 9.44:
            print("its 1st period")
        elif 9.49 <= time <= 10.58:
            print("its 2nd period")
        elif 10.58 < time <= 11.08:
            print("its break")
        elif 11.13 <= time <= 12.22: 
            print("its 3rd period")
        elif 12.22 < time <= 12.52:
            print("its lunch")
        elif 12.57 <= time <= 14.06: 
            print("its 4th period")
        elif 14.06 < time <= 14.31: 
            print("its office hours")
        elif 14.36 < time <= 15.45: 
            print("its 5th period")
        elif time > 15.45:
            print("school is over :)")
        else:
            print("its passing period")  
            
        
schedule(time)
what day is it? type the first letter, or 'th' for thursday
its wednesday
what time is it?
the time is:  10.15
its 1st period