CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium
A developer is designing a function that needs to accept an unknown number of arguments. For example, it might sum two numbers, or three numbers, or even ten numbers. Which programming concept allows a function to handle a variable number of input parameters?
- ADefault parameters
- BRecursion
- CFunction overloading
- DVariable-length argument lists
Show answer & explanationAnswer & explanation
Correct answer: D. Variable-length argument lists
Variable-length argument lists (often called variadic functions, *args in Python, or rest parameters in JavaScript) allow a function to accept an arbitrary number of arguments, which are then typically processed as a collection within the function.
Why the other options are wrong
- A. Default parameters provide a fallback value if an argument is not passed, but don't allow an unknown number of arguments.
- B. Recursion is a function calling itself, not related to the number of input parameters.
- C. Function overloading allows multiple functions with the same name but different parameter types/counts, not a single function with variable arguments.
Variable-Length Arguments
A feature in programming languages that allows a function to accept an unspecified or arbitrary number of arguments. These arguments are typically collected into a data structure like a tuple or array within the function.
- Allows flexible function signatures
- Useful for functions like 'sum' or 'print'
- Syntax varies by language (e.g., *args, ...rest)
Memory trick: Unknown number of args? Use a Variable list!