Subprograms unit 2 Flashcards
(11 cards)
What is a subprogram?
A section of code within a larger program that performs a specific task.
What is a function?
A subprogram that returns a value to the main program.
What is a procedure?
A subprogram that does not return a value to the main program.
What are the benefits of using subprograms?
- Makes code easier to manage.
- Improves reusability.
- Makes code easier to read and debug.
- Allows for modular design.
What is a parameter?
A value that is passed into a subprogram when it is called.
What is the difference between local and global variables?
Local variables can only be used inside the subprogram.
Global variables can be used anywhere in the program.
Why is using local variables better in subprograms?
It prevents accidental changes to data in other parts of the program.
What does “passing data into and out of subprograms” mean?
Sending input values into the subprogram (parameters) and possibly getting a result back (return value).
What is a user-defined subprogram?
A function or procedure that you create yourself for a specific task.
What is a pre-existing subprogram?
A built-in function provided by the programming language (e.g. len(), print() in Python).
Example: Define a simple Python function that returns the square of a number.
def square(num):
return num * num