Variadic Functions (...)
The dots parameter lets a function accept any number of extra inputs, like a bag that can hold as many groceries as you bring. It passes them along to other functions.
In this page:
Syntax
function_name <- function(...) {
args <- list(...)
# body using args
}
Variadic Functions (...)
The special ... parameter lets a function accept any number of additional arguments, which can be forwarded to another function or collected into a list with list(...).
Many built-in functions, like cat() and paste(), rely on ... to accept a flexible number of inputs. This is R's mechanism for variable-length argument lists.
Note:
Use list(...) inside a function body to inspect or iterate over whatever extra arguments were passed in.
Example: Variadic Functions (...)
sum_all <- function(...) {
values <- c(...)
sum(values)
}
cat(sum_all(1, 2, 3), "\n")
cat(sum_all(10, 20, 30, 40), "\n")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Trying to read
...like a normal variable, as inlength(...), instead oflist(...)orc(...). - Passing named arguments in
...and losing the names when usingc(...). - Placing parameters after
..., where they can only be matched by their full name.
Chapter Summary
- Functions are defined with arguments, default arguments, and return values.
- Scope determines where variables are visible.
- Anonymous functions and variadic functions (
...) make functions flexible.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: