Introduction to Subprograms
Sub-programs, also known as subroutines are self-contained blocks of code that perform a specific task within a larger program. They are designed to enhance modularity, reusability, and maintainability of code.
Subprograms are first defined within the program. Once the function has been defined it can be executing anywhere within the program by calling the function, usually be writing the subprogram name, followed parentheses.
def greeting(): #Here we define the subprogram
print("hello world")
greeting() #Here we call the subprogram
Subprograms can be divided into two types: functions and procedures.
What is a subprogram?
Functions
A function is a type of subprogram that performs a specific task and returns a value after execution.
Functions return a result after execution, which can be used elsewhere in the program.Since functions return values, they can be used within expressions (e.g., mathematical calculations).
Example
def add(a, b):
return a + b
result = add(3, 5) # Returns 8
Procedures
A procedure (often called a subroutine) is a type of subprogram that performs a task but does not return a value. Procedures do not return a result after execution; their primary goal is to carry out a task.
Procedures are often used for performing actions like printing, modifying global variables, or interacting with the user.
Example Procedure
def greet(name):
print(f"Hello, {name}!")
greet("Maria") # Outputs: Hello, Maria!
What is the difference between a function and a procedure?
Which of the following programming constructs allows code to be written and tested independently?
Advantages of subprograms
Reusability
Subprograms can be reused in different parts of the program or in other programs, eliminating the need for duplicating code and saving development time.
Modularity
Subprograms allow for the division of complex tasks into smaller manageable units, making the code easier to understand and maintain.
Abstraction
Subprograms reduce complexity by hiding the details of their implementation, allowing the programmer to focus on the high-level functionality.
Code organization
Subprograms help in organizing code into logical sections, making it easier to navigate and debug.
What are the advantages of using subprograms?
Review: Fill in the Blanks
When calling a function or procedure, the are the actual values passed to the . This allows us to provide different inputs and obtain different outputs based on the values passed. s are special types of that call themselves within their own code. This enables solving complex problems in a more elegant and concise manner.
The of a subprogram defines the visibility and lifetime of variables within that subprogram. are variables defined within a function or procedure and can only be accessed from within that subprogram. These variables have a limited scope, meaning they are only available during the execution of the subprogram.
To handle data flow and control between subprograms, we use statements such as the and . The return statement is utilized in functions to send back a value to the calling code. On the other hand, the call statement is used to invoke a procedure or function, passing the required arguments.
In essence, functions and , along with parameters, arguments, recursion, scope, return statements, call statements, and local variables, are fundamental building blocks of modular programming. By leveraging these concepts, we can break down complex problems into manageable sections and promote code reusability, readability, and maintainability.
Complete! Ready to test your knowledge?
Subprograms
- Introduction to Subprograms
- Functions
- Procedures
- Advantages of subprograms