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 sequential 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 psuedocode to python
def convertFahrenheit(temperature):
    celsius = (temperature - 32) * 5/9
    return celsius

outsideTemp = int(input('what is the temperature outside?'))

convertFahrenheit(outsideTemp)
22.22222222222222

Developing Procedures

Slide 8:

Picking a ___ 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
def retakeGrader(currentPoints, quizGrade, questions):
    print('Your original grade was: ' + str(grade) + '%')
    currentGrade = currentPoints / questions
    currentGrade = currentGrade * 100
    if currentGrade > quizGrade:
        quizGrade = currentGrade
        print('Your new grade is: ' + str(quizGrade) + '%')
    else:    
        print('Your grade did not change')

pts = int(input('How many points did you get? (on the retake)'))
grade = int(input('What was your original grade? (percent)'))
qs = int(input('How many questions were on the retake?'))

retakeGrader(pts, grade, qs)
Your original grade was: 80%
Your new grade is: 90.0%

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 modularity
  • A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program reuse, which helps manage complexity

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.
a = ''
vf = ''
vi = ''
t = ''
x = ''

qList = {'want':'What is the desired value?\n(a, vf, vi, t, or x)', 
         'a':'What is the acceleration?\n(if unknown, input "?")', 
         'vf':'What is the final velocity?\n(if unknown, input "?")',
         'vi':'What is the initial velocity?\n(if unknown, input "?")',
         't':'What is the change in time?\n(if unknown, input "?")',
         'x':'What is the change in position?\n(if unknown, input "?")'}

def floatConverter(var):
    if var == "?":
        return(var)
    else:
        return float(var)

def aSolve(vf, vi, t, x):
    if vf == '?':
        return 2*(x - vi * t)/t**2
    elif t == '?':
        return (vf**2 - vi**2)/(2 * x)
    elif vi == '?':
        return 2*(x - ((2 * x)-(vf * t)))/t**2
    elif x == '?':
        return (vf**2 - vi**2)/(2 * x)
    else:
        return "Unsolvable"
        
def vfSolve(a, vi, t, x):
    if a == '?':
        return ((2*x)/t)-vi
    elif vi == '?':
        return (x-((1 / 2) * a * t ** 2))/t + a * t
    elif t == '?':
        print("Note: This is the absolute value; remember to check directionality.")
        return (vi**2+2*a*x)**(1/2) 
    elif x == '?':
        return vi + a * t
    else:
        return "Unsolvable"

def viSolve(a, vf, t, x):
    if a == '?':
        return (2 * x) / t - vf
    elif vf == '?':
        return (x-(.5*a*t**2))/t
    elif t == '?':
        print("Note: This is the absolute value; remember to check directionality.")
        return (vf**2-2*a*x)**(1/2)
    elif x == '?':
        return vf - a * t
    else:
        return "Unsolvable"

def xSolve(a, vf, vi, t):
    if a == '?':
        return t * (vf+vi)/t
    elif vf == '?':
        return vi*t+.5*a*(t**2)
    elif vi == '?':
        return t * (2*vf - a * t)/2
    elif t == '?':
        return (vf**2 - vi**2)/(2*a)
    else:
        return "Unsolvable"
    
def tSolve(a, vf, vi, x):
    if a == '?':
        return (2*x)/(vi+vf)
    elif vf == '?':
        return ((vi**2+2*a*x)**(1/2)-vi)/a # idk if this works 100% of the time but oh well boo hoo cry about it
    elif vi == '?':
        return (vf -(vf**2-2*a*x)**(1/2))/a # also dont know
    elif x == '?':
        return (vf - vi)/a
    else:
        return "Unsolvable"

def kinematicCalculator(want):
    if want == 'a':
        vf = floatConverter(input(qList['vf']))
        vi = floatConverter(input(qList['vi']))
        t = floatConverter(input(qList['t']))
        x = floatConverter(input(qList['x']))
        
        return aSolve(vf, vi, t, x)
    
    elif want == 'vf':
        a = floatConverter(input(qList['a']))
        vi = floatConverter(input(qList['vi']))
        t = floatConverter(input(qList['t']))
        x = floatConverter(input(qList['x']))
        
        return vfSolve(a, vi, t, x)
    
    elif want == 'vi':
        a = floatConverter(input(qList['a']))
        vf = floatConverter(input(qList['vf']))
        t = floatConverter(input(qList['t']))
        x = floatConverter(input(qList['x']))
        
        return viSolve(a, vf, t, x)
    
    elif want == 't':
        a = floatConverter(input(qList['a']))
        vf = floatConverter(input(qList['vf']))
        vi = floatConverter(input(qList['vi']))
        x = floatConverter(input(qList['x']))
        
        return tSolve(a, vf, vi, x)

    elif want == 'x':
        a = floatConverter(input(qList['a']))
        vf = floatConverter(input(qList['vf']))
        vi = floatConverter(input(qList['vi']))
        t = floatConverter(input(qList['t']))
        
        return xSolve(a, vf, vi, t)
    
    else:
        print('Please input a valid variable.\n(a, vf, vi, t, or x)')
    
want = input('What is the desired value?\n(a, vf, vi, t, or x)')

print(want + " = " + str(kinematicCalculator(want)))

# I will convert this to javascript at another time to make a user-friendly version, but for now, this works.
t=1.4285714285714286