Calling Procedures

  • 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 Cont.

  • 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

Problem #1 (5min)

  • 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
temperature = input()

def convertFahrenheit (temperature):
	celsius = temperature - 32
	celsius = celsius  * 5/9
	return celsius
	
outsideTemp = input("What is the temperature outside?")
outsideTemp = (convertFahrenheit(int(outsideTemp)))
print(outsideTemp)

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.

Writing Procedures

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 pseudocode

currentGrade = input()
quizGrade = input()

def convertGrade(currentPoints):
    currentGrade = currentPoints / 40
    currendGrade = currentGrade * 100
    if currentGrade > quizGrade:
        return quizGrade

currentGrade = (convertGrade(int(currentGrade)))
print("Grade:", quizGrade)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
c:\Users\Luke Angelini\vscode\Luke1\_notebooks\2022-12-08-12-13-notes.ipynb Cell 6 in <cell line: 10>()
      <a href='vscode-notebook-cell:/c%3A/Users/Luke%20Angelini/vscode/Luke1/_notebooks/2022-12-08-12-13-notes.ipynb#W5sZmlsZQ%3D%3D?line=6'>7</a>     if currentGrade > quizGrade:
      <a href='vscode-notebook-cell:/c%3A/Users/Luke%20Angelini/vscode/Luke1/_notebooks/2022-12-08-12-13-notes.ipynb#W5sZmlsZQ%3D%3D?line=7'>8</a>         return quizGrade
---> <a href='vscode-notebook-cell:/c%3A/Users/Luke%20Angelini/vscode/Luke1/_notebooks/2022-12-08-12-13-notes.ipynb#W5sZmlsZQ%3D%3D?line=9'>10</a> currentGrade = (convertGrade(int(currentGrade)))
     <a href='vscode-notebook-cell:/c%3A/Users/Luke%20Angelini/vscode/Luke1/_notebooks/2022-12-08-12-13-notes.ipynb#W5sZmlsZQ%3D%3D?line=10'>11</a> print("Grade:", quizGrade)

ValueError: invalid literal for int() with base 10: ''

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

Procedural Abstractin Part 2

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

Homework

A procedure that can be used in math to find the area of a circle.

pi = 3.14

def showAreaOfCircle(radius):
    area = pi * radius * radius
    print('Area: ' + str(area))

showAreaOfCircle(10)
Area: 314.0