Core Concepts | .9 | .11 | .12 | .13 | .14 | .15 | .16 | .17 | .18 |
3.13 Developing Procedures
Student led teaching on Developing Procedures. Learn how to create functions or methods that encapsulate specific tasks.
Developing Procedures and Procedural Abstraction
Procedures are an example of abstraction.
(In fact, it’s part of a special form of abstraction known as procedural abstraction.)
This is because you can call a procedure without knowing how it works. A common built-in procedure in programming languages is the print() procedure.
Procedures allow you to solve a large problem based on the solution to smaller sub-problems.
When you divide a computer program into separate sub-programs, it’s known as modularity. Procedures can also help you simplify your code and improve its readability. Instead of this:
first_number = 7
second_number = 5
sum_value = first_number + second_number
print (sum_value)
first_number = 8
second_number = 2
sum_value = first_number + second_number
print (sum_value)
first_number = 9
second_number = 3
sum_value = first_number + second_number
print (sum_value)
12
10
12
You can do this:
def summing_machine(first_number, second_number):
sum_value = first_number + second_number
print (sum_value)
summing_machine(5, 7)
summing_machine(8, 2)
summing_machine(9, 3)
12
10
12
This is especially important in larger programs, where you might already be looking at hundreds of lines of code.
Procedures can be reused. Parameters usually represent general categories of data, such as numbers or strings.
This means you could use one procedure for a wide range of individual scenarios.
Remember that placing a return statement inside a procedure will cause an immediate return from the procedure back to where the procedure is called.
The return statement may appear at any point inside the procedure.
Procedural abstraction also allows programmers the flexibility to modify or fix procedures without affecting the whole program, as long as the procedure continues to function in the same way.
Furthermore, they can make a change or edit once, in the procedure, and it will be implemented everywhere the procedure is called.
Example: Create a Python program that generates multiplication tables for a specific number up to a specified limit.
def generate_multiplication_table(number, limit):
"""
Generates a multiplication table for the given number up to the specified limit.
:param number: The number for which to generate the table.
:param limit: The upper limit of the table (e.g., how far the multiplication will go).
"""
print(f"Multiplication Table for {number} (Up to {limit}):\n")
for i in range(1, limit + 1):
result = number * i
print(f"{number} x {i} = {result}")
# Usage
generate_multiplication_table(5, 10)
Multiplication Table for 5 (Up to 10):
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Anatomy of Python Classes
Class Definition: Classes are defined using the class keyword, followed by the class name, and a colon.
The class body is indented beneath the class definition.
class MyClass:
# class body
Pass
Attributes: These are variables that belong to a class.
They can be accessed using the dot notation on an instance of the class.
class MyClass:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
Methods: These are functions that are defined within a class.
They can perform specific operations on the object or modify its attributes.
class MyClass:
def my_method(self):
# method body
pass
Constructor: The init method is a constructor in Python classes.
It is called when an object is created and allows the class to initialize the object’s attributes.
class MyClass:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
Self: It represents the instance of the class and is used to access the class’s attributes and methods.
class MyClass:
def my_method(self):
print(self.attribute1)
Hacks
Review each of the following and produce…
- A Python function called procedural_abstraction that demonstrates procedural abstraction by performing a specific task. Your function should take at least one parameter and return a result.
- A Python function called summing_machine that takes two parameters, first_number and second_number, and returns the sum of these numbers. Use this function to calculate the sum of 7 and 5. Print the result.