Procedure
A procedure is a type of subprogram that carries out a task, but does not return a value.
You write the code for a procedure once, and then you can call (use) it whenever you need that task to be done.
def add_nums():
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
total = num1 + num2
print("The total is", total)
# Main
add_nums()
def add_nums(num1, num2):
total = num1 + num2
print("The total is", total)
# Main
add_nums(6,4)
Key features of procedures
-
Has a name so it can be called when needed
-
Can take inputs (called parameters)
-
Does not return a value to the main program
-
Useful for repeating tasks without writing the same code again
Why use procedures?
-
Organises your code into smaller, easy-to-understand sections
-
Avoids repetition – write once, use many times
-
Makes your code easier to test and fix
-
Helps with teamwork, as different people can work on different procedures concurrently