Procedures are essential building blocks in programming. They help break large projects into smaller, manageable parts. Therefore, learning about procedures early on makes it easier to organize code, solve problems, and collaborate with others. This article focuses on the concept of what is a procedure, how to recognize one, and how it differs from other important programming terms. Additionally, it will explore how procedures handle parameters, arguments, and return values.
What We Review
What Is a Procedure?
A procedure is a named group of programming instructions that can include parameters and return values. Think of it like a mini recipe in a cookbook: the procedure name indicates what the recipe does, while the parameters specify the ingredients. Following the recipe step by step, you execute the instructions inside the procedure in sequence.
In most programming languages, a procedure is defined once and can be reused multiple times. This reuse saves time and reduces repetitive code. For example, consider a simple addNumbers procedure that sums two values. This procedure can be called whenever an addition is needed, rather than rewriting those same instructions over and over.
Procedure Example
PROCEDURE addNumbers(a, b)
{
sum ← a + b
RETURN(sum)
}
Understanding Parameters and Arguments
A procedure’s header defines the variable names called parameters. They are placeholders that represent input values needed by the procedure. In contrast, arguments are the actual values passed to those parameters when the procedure is called.
To illustrate, imagine a vending machine: the parameters are the labeled slots (like “slot A” and “slot B”) that await coins. The arguments are the actual coins inserted. Therefore, if the procedure has parameters named x and y, and you call the procedure with arguments 3 and 5, the procedure will work with those specific values.
Procedure Example with Input
PROCEDURE greetUser(username)
{
DISPLAY("Hello, " + username)
}
Here, username is the parameter. When greetUser(“Alex”) is called, the argument is “Alex,” and the procedure displays “Hello, Alex.”
Calling Procedures
Calling a procedure tells the program to pause its current sequence of instructions and run the procedure’s instructions instead. Once the procedure finishes or encounters a RETURN statement, flow of control continues from the next line after the call.
For instance, imagine a teacher who steps out of a classroom (the main program flow) to ask the principal (the procedure) a question. The teacher waits while the principal addresses the question. After receiving the answer, the teacher resumes the lesson.
Step-by-Step Procedure Call
- The main program executes until it reaches addNumbers(4, 7).
- Control jumps to PROCEDURE addNumbers(a, b).
- Inside the procedure, sum ← a + b, which becomes sum ← 4 + 7.
- The procedure then returns the result.
- Execution goes back to the line after addNumbers(4, 7) in the main program.
Returning Values from Procedures
A RETURN statement returns a value to the point where the procedure calls it. This is like an email message sent back with relevant information. Consequently, procedures that return values perform complex operations in a neat and organized way.
Moreover, a procedure can return any data type that the language supports, such as a number, text, or even a boolean. Consider a procedure that calculates the area of a rectangle. When the procedure finishes, it returns the computed area, and the main program can store or display it.
Procedure Example Returning a Value
PROCEDURE calculateArea(length, width)
{
result ← length * width
RETURN(result)
}
When rectangleArea ← calculateArea(10, 5) is called, rectangleArea will hold the value 50.
Procedures vs. Functions vs. Methods
In some programming contexts, the term “procedure” is used interchangeably with “function” or “method.” However, there are subtle differences. Typically, a function returns a value, while a procedure may or may not return one. In object-oriented languages like Java, programmers typically refer to class-specific procedures as “methods.”
Furthermore, many high-level languages use the keyword function rather than procedure. For instance, JavaScript uses function to define reusable blocks of code. However, in Java, these blocks of code are called methods when associated with a class. Despite these variations in terminology, the core idea remains the same: programmers use named sets of instructions that they can call multiple times.
Function vs Method
- function in JavaScript (not tied to a class)
- method in Java (defined inside a class)
- procedure is a broader term, used in many educational settings
Practical Applications of Procedures
Procedures help structure code in a repeatable way. For example, one can easily debug a large program by isolating problematic logic inside a single block. As a result, development becomes more efficient. Additionally, this approach helps organize code by grouping related tasks by functionality.
Real-world tasks showcase the same concept. A washing machine has pre-set “cycles” (like “heavy wash” or “delicate”) that encapsulate specific instructions. Each cycle is like a procedure that can be used repeatedly.
Therefore, whenever a task appears multiple times within a program, creating a reusable block for it saves effort and reduces errors. In large-scale software, procedures enhance collaboration because multiple programmers can work on different parts of the code with minimal overlap.
Summary of Key Terms
Below is a quick reference list of important vocabulary. These terms play a big role in mastering procedures.
- Procedure – A named group of programming instructions that may have parameters and return values
- Parameter – An input variable defined in a procedure’s header
- Argument – The actual value passed to a procedure’s parameter
- Return – A statement causing a procedure to send a value back and resume the main program flow
- Function – Often used to refer to a reusable piece of code that returns a value
- Method – A reusable piece of code associated with a class in object-oriented programming
Conclusion
Procedures provide an efficient way to organize and reuse code. The ability to define and call procedures with parameters and return values allows for flexibility and clarity in a program. Consequently, programs become more manageable as tasks are split into smaller parts.
Begin practicing procedures by writing simple examples that accept inputs and return results. Try to convert repetitive tasks into reusable procedures. Next, test various input values to see how a procedure’s output changes. Lastly, consider exploring object-oriented languages to understand why methods exist within classes. With regular practice, the concept of procedures will become second nature, and future projects will benefit from this organized approach.
Sharpen Your Skills for AP® Computer Science Principles
Are you preparing for the AP® Computer Science Principles test? We’ve got you covered! Try our review articles designed to help you confidently tackle real-world AP® Computer Science Principles questions. You’ll find everything you need to succeed, from quick tips to detailed strategies. Start exploring now!
- AP® Computer Science Principles 3.9 Review
- AP® Computer Science Principles 3.10 Review
- AP® Computer Science Principles 3.11 Review
Need help preparing for your AP® Computer Science Principles exam?
Albert has hundreds of AP® Computer Science Principles practice questions and full-length practice tests to try out.