Procedural programming organises a program as a sequence of instructions grouped into procedures and functions. It is the style you have probably used most, and it is the foundation for the other paradigms.
Core ideas
- Sequence, selection and iteration: every procedural program is built from these three control structures.
- Procedures and functions: named blocks of code that do one job. A procedure performs actions; a function returns a value.
- Parameters: values passed into a subroutine. By value passes a copy, so the original is unchanged. By reference passes the variable itself so the subroutine can change it.
- Local and global variables: a local variable exists only inside its subroutine; a global variable can be used anywhere. Prefer local variables and parameters, because globals can be changed from anywhere and cause hard-to-find bugs.
- Top-down design: break the task into subroutines, then call them from a main program.
FUNCTION CalcArea(Length : REAL, Width : REAL) RETURNS REAL
RETURN Length * Width
ENDFUNCTION
PROCEDURE ShowArea(Length : REAL, Width : REAL)
OUTPUT "Area is ", CalcArea(Length, Width)
ENDPROCEDURE
CALL ShowArea(4.5, 2)
By reference example:
PROCEDURE Swap(BYREF A : INTEGER, BYREF B : INTEGER)
Temp <- A
A <- B
B <- Temp
ENDPROCEDURE
Why use subroutines
- Reuse: write once, call many times.
- Readability: meaningful names make the main program read like a plan.
- Testing: each routine can be tested alone.
- Teamwork: different programmers write different routines.
- Maintenance: a fix in one place applies everywhere.
Good style includes meaningful identifiers, comments explaining why, consistent indentation, and constants for fixed values instead of “magic numbers”. Each subroutine should do one thing and have a clear interface, which supports modular design.
Limits
In large programs, many routines share data structures, and changes to that data can ripple through the code. Object-oriented programming responds to this by bundling data with the code that works on it. Procedural code remains ideal for scripts, small tools, and systems where performance and simplicity matter.
Check your understanding
What is the difference between a procedure and a function?
- A function returns a value; a procedure does not
- A procedure returns a value; a function does not
- They are identical
- Functions have no parameters
Answer: A function returns a value; a procedure does not. Both are subroutines.
What happens when a parameter is passed by value?
- The subroutine works on a copy
- The original variable is changed
- The program crashes
- The value becomes global
Answer: The subroutine works on a copy. The original is unchanged.
Why are global variables discouraged?
- They can be changed anywhere, causing hard-to-trace bugs
- They are slower to type
- They use hardware
- They cannot hold numbers
Answer: They can be changed anywhere, causing hard-to-trace bugs. Use parameters and locals instead.
Which three structures form all procedural programs?
- Sequence, selection and iteration
- Class, object and method
- Input, output and storage
- Compile, link and run
Answer: Sequence, selection and iteration. Subroutines organise them.
