CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium
A developer is writing a program that processes a list of customer names. The program needs to define a reusable block of code that takes a customer's name as input, formats it, and then prints a personalized greeting. This block of code should be callable multiple times from different parts of the program. What programming construct best describes this reusable block?
- AFunction
- BConstant
- CVariable
- DLoop
Show answer & explanationAnswer & explanation
Correct answer: A. Function
A function (or subroutine/method) is a named, reusable block of code that performs a specific task. It can accept input (parameters) and often returns a result, making it ideal for tasks like formatting and printing a personalized greeting that needs to be called repeatedly.
Why the other options are wrong
- B. A constant is a fixed value that cannot be changed, not a block of code.
- C. A variable stores a single piece of data, not a block of reusable code.
- D. A loop repeats a block of code multiple times, but it is not itself the reusable block that performs a specific task with input.
Function (Programming)
A self-contained block of code designed to perform a specific task. Functions can take inputs (arguments/parameters) and may return a value. They promote code reusability and modularity.
- Reusable block of code.
- Performs a specific task.
- Can take input and return output.
Memory trick: Functions are 'fun' because you can 'call' them again and again.