Developing a procedure means writing the code for what it does.
Calling a procedure means using it in your program.
You must develop (define) a procedure before you can call it.
JavaScript
// Developing (defining) the procedure
function double(num) {
console.log(num * 2);
}
// Calling the procedure
double(5);
Developing a procedure is when you write the function itself, including its name, parameters, and instructions.
Example: function double(num) { console.log(num * 2); }
Calling a procedure is when you use its name to make it run.
Example: double(5);
Python
# Developing (defining) the procedure
def double(num):
print(num * 2)
# Calling the procedure
double(5)
Developing a procedure is when you write the function, including its name, parameters, and instructions.
Example: def double(num): print(num * 2)
Calling a procedure is when you use its name to make it run.
Example: double(5)
What is Procedural Abstraction?
Key Points
Procedures are a special form of abstraction
You can use a procedure without knowing its inner details
Built-in procedures like print() are common examples
JavaScript
// You can use built-in procedures like console.log()
console.log("Hello, world!");
In JavaScript, console.log() is a built-in procedure.
You use it to print messages, without needing to know how it works inside.
This is procedural abstraction: you use the procedure, but don’t need to see its code.
Python
# You can use built-in procedures like print()
print("Hello, world!")
In Python, print() is a built-in procedure.
You use it to display messages, without needing to know how it works inside.
This is procedural abstraction: you use the procedure, but don’t need to see its code.
Solving Problems with Procedures
Key Points
Break big problems into smaller sub-problems
Solve each sub-problem with a procedure
Combine procedures to solve the whole problem
JavaScript
// Example: Choose-your-own-adventure game
function displayChoice(choice) {
console.log("You chose: " + choice);
}
function trackProgress(step) {
// ...code to track progress
}
// Combine procedures to build the game
displayChoice("forest");
trackProgress(1);
In JavaScript, you can break a big problem (like a game) into smaller pieces.
Write a procedure for each sub-problem (like displaying choices or tracking progress).
Call these procedures to build your program.
This makes your code easier to manage and understand.
Python
# Example: Choose-your-own-adventure game
def display_choice(choice):
print("You chose:", choice)
def track_progress(step):
# ...code to track progress
pass
# Combine procedures to build the game
display_choice("forest")
track_progress(1)
In Python, you can break a big problem (like a game) into smaller pieces.
Write a procedure for each sub-problem (like displaying choices or tracking progress).
Call these procedures to build your program.
This makes your code easier to manage and understand.
Modularity
Key Points
Divide your program into separate sub-programs
Each procedure solves a specific part of the problem
Modularity makes code easier to read and maintain
JavaScript
function add(a, b) {
return a + b;
}
function print_sum(a, b) {
console.log(add(a, b));
}
print_sum(7, 5);
print_sum(8, 2);
print_sum(9, 3);
In JavaScript, modularity means dividing your code into small, reusable procedures.
add() does the calculation.
print_sum() prints the result.
Call these procedures as needed.
This keeps your code organized and easy to update.
Python
def add(a, b):
return a + b
def print_sum(a, b):
print(add(a, b))
print_sum(7, 5)
print_sum(8, 2)
print_sum(9, 3)
In Python, modularity means dividing your code into small, reusable procedures.
add() does the calculation.
print_sum() prints the result.
Call these procedures as needed.
This keeps your code organized and easy to update.
Code Reuse and Readability
Key Points
Procedures can be reused with different inputs
Parameters make procedures flexible
Procedures simplify and clarify your code
JavaScript
function summingMachine(first, second) {
let sum = first + second;
console.log(sum);
}
summingMachine(5, 7);
summingMachine(8, 2);
summingMachine(9, 3);
In JavaScript, you can reuse procedures by calling them with different inputs.
Parameters let you use the same procedure for many cases.
Your code is shorter and easier to read.
This is especially helpful in large programs.
Python
def summing_machine(first, second):
sum_value = first + second
print(sum_value)
summing_machine(5, 7)
summing_machine(8, 2)
summing_machine(9, 3)
In Python, you can reuse procedures by calling them with different inputs.
Parameters let you use the same procedure for many cases.
Your code is shorter and easier to read.
This is especially helpful in large programs.
Return Statements
Key Points
Use return to send a value back
Return ends the procedure immediately
You can return from anywhere inside the procedure
JavaScript
function multiply(a, b) {
if (a === 0 || b === 0) {
return 0;
}
return a * b;
}
console.log(multiply(3, 4)); // 12
console.log(multiply(0, 5)); // 0
In JavaScript, return sends a value back and ends the procedure.
You can use return anywhere in your function.
After return, the function stops and goes back to where it was called.
This lets you control what your procedure gives back.
Python
def multiply(a, b):
if a == 0 or b == 0:
return 0
return a * b
print(multiply(3, 4)) # 12
print(multiply(0, 5)) # 0
In Python, return sends a value back and ends the procedure.
You can use return anywhere in your function.
After return, the function stops and goes back to where it was called.
This lets you control what your procedure gives back.
Flexibility and Maintenance
Key Points
Procedural abstraction lets you change code in one place
You can fix or improve a procedure without changing the whole program
Updates apply everywhere the procedure is used
JavaScript
function greet(name) {
return "Hello, " + name + "!";
}
// If you want to change the greeting, update the function once
In JavaScript, if you need to fix or improve a procedure, you only change it in one place.
All calls to the procedure use the updated version.
This makes your code easier to maintain and update.
Procedural abstraction helps keep your programs flexible.
Python
def greet(name):
return "Hello, " + name + "!"
# If you want to change the greeting, update the function once
In Python, if you need to fix or improve a procedure, you only change it in one place.
All calls to the procedure use the updated version.
This makes your code easier to maintain and update.
Procedural abstraction helps keep your programs flexible.
Try It Yourself: Write and Call a Procedure
Key Points
Practice creating your own procedure
Call your procedure with different values
Try It Yourself (JavaScript)
Write a function called greet that takes a name and prints 'Hello, ' plus the name. Then call your function with the name 'Alex'.
Try It Yourself (Python)
Write a function called greet that takes a name and prints 'Hello, ' plus the name. Then call your function with the name 'Alex'.