Subprograms
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
A function is a type of subprogram that returns a after execution, which can be used elsewhere in the program. Since functions return values, they can be used within expressions (e.g., mathematical calculations). In contrast, a procedure is a block of code that performs a task but does not return a value. Procedures are often used for performing actions like , modifying global variables, or interacting with the user.
Subprograms allow for the division of complex tasks into smaller manageable units, making the code easier to understand and . They can be reused in different parts of the program or in other programs, eliminating the need for duplicating code and saving . Moreover, subprograms reduce complexity by hiding the details of their , allowing the programmer to focus on the high-level functionality. Additionally, they help in organizing code into logical sections, making it easier to and debug.
Complete! Ready to test your knowledge?
Subprograms
- Introduction to Subprograms
- Functions
- Procedures
- Advantages of subprograms