Student Teaching Lesson 3.12 & 3.13
Student Teaching Notes and Homework (3.12 - 3.13)
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. 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
outsideTemp = int(input("What is the temperature outside in Fahrenheit? (Don't include units)"))
def convertFahrenheit(temperature):
celsius = temperature - 32
celsius = celsius * (5/9)
celsius = round(celsius)
print(celsius)
convertFahrenheit(outsideTemp)
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
currentPoints = int(input("How many points did you earn on the retake? (Positive integer"))
quizGrade = int(input("How many points did you earn previously for the quiz? (Positive integer)"))
quizTotal = int(input("How many points was the quiz out of? (Positive integer"))
def quizReplace(currentPoints, quizGrade, quizTotal):
quizGrade = (quizGrade/quizTotal) * 100
currentGrade = currentPoints / quizTotal
currentGrade = currentGrade * 100
if (currentGrade > quizGrade):
quizGrade = currentGrade
return quizGrade
print("Your new grade on the quiz is " + str(int(quizReplace(currentPoints, quizGrade, quizTotal))) + "%")
# inputted: currentPoints = 20
# quizGrade = 60
# quizTotal = 25
Practice Problem #1
To move the robot 180 degrees in place, these are the commands needed
PROCEDURE rotate180()
ROTATE_RIGHT()
ROTATE_RIGHT()
Practice Problem #2
PROCEDURE aroundBlock()
ROTATE_LEFT()
MOVE_FORWARD()
ROTATE_RIGHT()
MOVE_FORWARD()
MOVE_FORWARD()
ROTATE_RIGHT()
MOVE_FORWARD()
ROTATE_LEFT()
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 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.
# This procedure calculates a curved grade based on the highest score in your class.
# It also tells you your new score as a percentage and a letter grade.
# Calculates how your grade is curved
# (assuming the highest score out of the class is set as the new total # of points)
def curveCalculator(score, highScore):
# error-proofing the procedure
if (score < 0) or (highScore < 0):
print("Positive integers only!")
elif (score > highScore):
print("Your score cannot be higher than the highest score in the class.")
else:
# The actual calculations
return (score / highScore) * 100
# Calculates your letter grade after the curve
def letterGradeCalculator(percent):
if (percent < 60):
return "F"
elif (percent >= 60) and (percent < 70):
return "D"
elif (percent >= 70) and (percent < 80):
return "C"
elif (percent >= 80) and (percent < 90):
return "B"
elif (percent >= 90) and (percent < 100):
return "A"
else:
return "A+"
# Takes user inputs for their score and the class's highest score and stores as variables
score = int(input("Enter how many points you scored."))
highScore = int(input("Enter the highest number of points that someone in your class scored."))
percent = curveCalculator(score, highScore)
if type(percent) == float: # checks if percent is a number (invalid inputs will not meet this condition)
Grade = letterGradeCalculator(percent)
print("Your new score is " + str(round(percent)) + "%")
print("That's a(n) " + str(Grade) + "!")
else:
print("Try again with valid numbers.")
# For the outputs shown below, I inputted the following:
# score = 46
# highScore = 48