Vocab

  • A procedure is a named group of programming instructions that may have parameters and return values
  • Parameters are input values of a procedure
  • Arguments specify the values of the parameters when procedure is called
  • procedural abstraction: provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it; naming and calling a prewritten procedure
  • modularity: Subdivision of a program into separate subprograms

3.12: Calling Procedures Part 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.

Calling Procedures Part 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

procName (arg1, arg2, ...) --> result

Calling Procedures Practice

Add a procedure call to the code segment below that will convert the outside temperature to Celsius. Assume the Temperature outside is Fahrenheit. The procedure convertFahrenheit is intended to convert from Fahrenheit to Celsius. You will need to convert the pseudo code to Python

def convertFahrenheit (temperature): # function that converts the variable temperature to celsius
    celsius = temperature - 32
    celsius = celsius * 5/9 
    return celsius

outsideTemperature = input("What is the temperature outside?")
outsideTemperature = (convertFahrenheit(int(outsideTemperature))) # call the function and assign it to the variable "outsideTemperature". 'outsideTemperature' is the temperature argument
print(outsideTemperature)
26.11111111111111

3.13: Developing Procedures

  • There are 2 different types of procedures- ones that return a value and those that simply execute a block of statements
  • Picking a descriptive name is important in case you revisit the code later on (separate words with capitals)
  • Steps of developing procedure: picking 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.

Practice Writing Procedures

quizGrade = 35

def changeQuizGrade (currentPoints):
    currentGrade = currentPoints / 40
    currentGrade = currentGrade * 100
    return currentGrade

newPoints = int(input(print("How many points did you get on your most recent quiz attempt, out of 40?")))
print(str(newPoints) + "/40")
newGrade = (changeQuizGrade(int(newPoints)))

if newPoints > quizGrade:
    newQuizGrade = newGrade
    print("Your new grade is " + str(newQuizGrade))
else:
    print("Your quiz score is still " + str(quizGrade))
How many points did you get on your most recent quiz attempt, out of 40?
38/40
Your new grade is 95.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
  • 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 prewritten procedure
  • Making sure to include the right arguments so the procedure can do what it's supposed to do is crucial

Practice Procedural Abstraction

Determine the outcome of this code segment:

quizAverage = 74

currentScore = 35

totalPoints = 40

PROCEDURE updateQuiz(quizAverage, currentScore, totalPoints)

tempAverage <-- currentScore / totalPoints

tempAverage <-- tempAverage * 100

if tempAverage > quizAverage

quizAverage <-- tempAverage

return quizAverage

The output should be 87.5

Updating the Contents of a Procedure

  • It is possible to simply update the contents of the procedure (possibly with another procedure) if you know how to call that specific procedure
  • For example, we are able to update the procedure updateQuiz with the round() procedure by simply knowing how to call it:

quizAverage = 74

currentScore = 35

totalPoints = 40

PROCEDURE updateQuiz(quizAverage, currentScore, totalPoints)

tempAverage <-- currentScore / totalPoints

tempAverage <-- tempAverage * 100

tempAverage <-- round(tempAverage) <---------- updating the procedure updateQuiz using round()

if tempAverage > quizAverage

quizAverage <-- tempAverage

return quizAverage