The examples below use AP CSP Pseudocode. For Java, see Parameters as abstractions.
Abstractions in computer science hide details to manage complexity.
Commonly used abstractions include:
- Variables
- Procedures (often called methods or functions)
- Parameters (see details below)s
- Lists
Many procedures can be written more generally by using one or more parameters.
The example below assumes an understanding of:
- Writing and calling Procedures with parameters in AP CSP Pseudocode
- Traversals in AP CSP Pseudocode
Example: Searching for a value in a list
With a key
parameter
PROCEDURE search(list, key)
{
FOR EACH element IN list
{
IF(element = key)
{
RETURN(true)
}
}
RETURN(false)
}
The search
procedure returns true
if its parameter key
is in its parameter list
, false
otherwise.
Without a key
parameter
PROCEDURE searchForFive(list)
{
FOR EACH element IN list
{
IF(element = 5)
{
RETURN(true)
}
}
RETURN(false)
}
PROCEDURE searchForTen(list)
{
FOR EACH element IN list
{
IF(element = 10)
{
RETURN(true)
}
}
RETURN(false)
}
The searchForFive
and searchForTen
procedure implement the same algorithm as the search
procedure, but without the parameter key
.
Without the parameter key
, searchForFive
must compare each value in list
to 5
and searchForTen
must compare each value to 10
. Searching for a different value, such as 15
, would require writing another version.
The parameter manages complexity
Parameters are abstractions because they allow the code inside a method to be written more generally.
Related examples
Help & comments
Get help from AP CS Tutor Brandon Horn