Define and use a function

Resources | Subject Notes | Computer Science

Structured Programming: Functions

11.3 Structured Programming: Functions

Definition of a Function

A function is a reusable block of code designed to perform a specific task. It takes input (arguments), processes that input, and can optionally produce output (a return value). Functions are a fundamental concept in structured programming, promoting modularity, code reusability, and easier maintenance.

Benefits of Using Functions

  • Modularity: Functions break down complex programs into smaller, manageable units.
  • Reusability: A function can be called multiple times from different parts of the program, avoiding code duplication.
  • Readability: Functions make code easier to understand by giving meaningful names to blocks of code.
  • Maintainability: Changes to a function only need to be made in one place.
  • Abstraction: Functions hide the internal details of a task, allowing programmers to focus on what the function does rather than how it does it.

Function Definition

In most programming languages, a function is defined using a specific syntax that includes the function's name, a list of parameters (inputs), and the code block that performs the function's task. The return type (if any) is also specified.

Example (Python-like syntax):

def calculate_area(length, width):
    area = length * width
    return area

Function Call

To execute a function, you need to call it. This involves using the function's name followed by parentheses, and providing any required arguments.

Example (Python-like syntax):

rectangle_area = calculate_area(5, 10)
print(rectangle_area) 

Parameters and Arguments

Parameters are the variables listed in the function definition. They represent the inputs the function expects.

Arguments are the actual values passed to the function when it is called. Arguments are assigned to the corresponding parameters.

Example:

Parameter Argument
length 5
width 10

Return Value

A function can optionally return a value after it has completed its task. The return statement specifies the value that will be returned. If no return statement is used, or if the return statement is used without a value, the function typically returns None (in Python) or a similar null value in other languages.

Example Program (Conceptual - Pseudocode)

Let's consider a simple example of a function to calculate the area of a circle.

        Function CalculateCircleArea(radius)
            Area = $\pi \times radius^2$
            Return Area
        End Function

        // Main Program
        Set radius = 7
        CircleArea = CalculateCircleArea(radius)
        Display "The area of the circle is: " + CircleArea
    

Structured Programming and Functions

Functions are a key component of structured programming. They help to organize code into logical blocks, making programs easier to understand, debug, and modify. By breaking down a problem into smaller, manageable functions, programmers can create more robust and maintainable software.