Procedures and Sub-Procedures
What is a Procedure?
Procedure
A is a series of steps or actions that are followed to achieve a specific goal.
In computer science, procedures are often referred to as algorithms.
- A recipe is a classic example of a procedure.
- It provides a step-by-step guide to preparing a dish.
- To make a sandwich, you need to:
- Gather bread, butter, and filling.
- Spread butter on the bread.
- Add the filling.
- Place the second slice of bread on top.
- Cut the sandwich in half.
Steps to Identifying a Procedure
When you identify a procedure, you are essentially designing an algorithm that can be implemented in a program.
Understand the problem: Before you can identify a suitable procedure, you need to understand the problem fully.
Ask yourself:
- What is the goal?
- What constraints are there?
- What resources do I have?
Break down the problem: Divide it into smaller, manageable parts.
If you are asked to bake a cake, the smaller parts might include:
- Gathering ingredients
- Mixing the batter
- Baking the cake
- Decorating the cake
Identify the steps: List the specific actions needed to solve each part of the problem.
For mixing the batter, the steps might be:
- Crack the eggs into a bowl.
- Add sugar and butter.
- Mix until smooth.
Put the steps in order: Arrange the steps in the correct sequence.
- Arranging in order is crucial because the order can affect the outcome.
- For example, you can't bake the cake before mixing the batter.
Review and refine: go through the procedure to ensure it is complete and efficient.
Ask yourself:
- Are there any unnecessary steps?
- Is there a way to simplify the process?
Visualisation in flowchart: Block-Arrow-Block-Arrow
This method is often used in flowcharts to visually represent procedures.
Logging into a website:
- Enter username
- Enter password
- Click "Login"
- If credentials are correct, access the account
- If credentials are incorrect, display an error message
Sub-Procedures
Sub-procedures
Smaller, reusable blocks of code that help break down complex problems into manageable parts.
- Sub-procedures include functions and procedures.
- Functions return a value, while procedures do not.
- Think of sub-procedures like recipes in a cookbook.
- Once you have a recipe for making a cake, you can use it whenever you want to bake a cake, without having to reinvent the process each time.
A function to calculate the area of a rectangle can take the length and width as parameters, allowing it to work with any rectangle.
- Sub-procedures are fundamental to efficient problem-solving in programming.
- By breaking down tasks further and promoting reusability, they make complex problems manageable and pave the way for scalable, maintainable solutions.
Defining and Calling Sub-Procedures
- In most programming languages, you define a sub-procedure using a specific keyword.
- The sub-procedure is given a name (identifier) and may include parameters.
def my_function():
print("Hello World!")To use a sub-procedure, you call it by its name and provide any required arguments.
def say_hello(name): # pass name as argument
print("Hello " + name + "!")
say_hello("Alice") # invoke function
say_hello("Bob") # invoke function with different argument- Can you think of a time when following a procedure helped you solve a problem?
- Identify the procedure for making a cup of tea.
- Arrange the following steps in the correct order for washing a car:
- Rinse the car with water.
- Dry the car with a towel.
- Apply soap to the car.
- Scrub the car with a sponge.
- Rinse off the soap.