<< Hide Menu
2 min read•june 18, 2024
Minna Chow
Milo Chang
Minna Chow
Milo Chang
Procedures are an example of abstraction. (In fact, it's part a special form of abstraction known as procedural abstraction.) This is because you can call a procedure without knowing how it works. Indeed, you often do: 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. For example, my final Create project was a choose-your-own-adventure story. In order to make this story, I had to figure out how to let players choose what path they were going to take, how to display this path choice, how to track a player's progress in the game so their actions would have consequences... and so on. I solved the larger "problem" of writing a choose-your-own-adventure story by solving all these smaller problems with procedures I then combined.
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)
you get 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)
Isn't the second example a lot easier to understand? This is especially important in larger programs, where you might already be looking at hundreds of lines of code.
The beauty of procedures is how they 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.
© 2024 Fiveable Inc. All rights reserved.