Use the terminology associated with procedures and functions

Resources | Subject Notes | Computer Science

Structured Programming: Procedures and Functions

Structured Programming: Procedures and Functions

11.3 Structured Programming

Structured programming is a programming paradigm that emphasizes the use of control flow structures to create well-organized and readable code. It avoids the use of the GOTO statement, which can lead to spaghetti code that is difficult to understand and maintain. The core principles involve using sequences, selections (if-then-else), and iterations (loops) to control the flow of execution.

Procedures and Functions

Procedures and functions are fundamental building blocks of structured programming. They allow you to break down a complex task into smaller, more manageable units of code. Both procedures and functions encapsulate a sequence of instructions, providing a reusable block of code that can be called from different parts of the program.

Key Differences between Procedures and Functions

Feature Procedure Function
Return Value No explicit return value. May or may not return a value.
Usage Executed as a standalone block of code. Called as part of an expression; its return value can be used.
Purpose Typically performs a series of actions. Typically performs a calculation and returns a result.

Procedure Definition

A procedure is defined using the Procedure keyword (or a similar keyword depending on the programming language). It typically takes input parameters and performs a series of actions. The procedure is then called to execute the defined sequence of instructions.

Example (Conceptual):

  1. Procedure CalculateArea(width, height)
  2. Area = width * height
  3. Display "Area: " + Area

Function Definition

A function is defined using the Function keyword (or a similar keyword). Functions are designed to perform a specific calculation and return a value. They are used to modularize code and avoid repetition.

Example (Conceptual):

  1. Function CalculateArea(width, height)
  2. Area = width * height
  3. Return Area

Terminology

Understanding the following terminology is crucial for working with procedures and functions:

  • Parameters/Arguments: Variables that receive values passed to the procedure or function.
  • Return Value: The value that a function sends back to the calling code. Procedures typically don't have a return value.
  • Local Variables: Variables declared within the procedure or function; they are only accessible within that block of code.
  • Global Variables: Variables declared outside of any procedure or function; they are accessible from anywhere in the program.
  • Scope: The region of the program where a variable is accessible.

Benefits of Using Procedures and Functions

  1. Modularity: Breaking down a program into smaller, independent units.
  2. Reusability: Using the same code multiple times without rewriting it.
  3. Readability: Making code easier to understand and maintain.
  4. Reduced Complexity: Simplifying complex tasks by dividing them into smaller parts.

Using procedures and functions is a cornerstone of writing well-structured and maintainable code. They promote code reuse, improve readability, and make programs easier to debug.