← Back to R Course | Chapter 5: Functions | Lesson 7 of 7

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:

  1. Variadic Functions (...)
Syntax
r
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 (...)

markup
sum_all <- function(...) {
  values <- c(...)
  sum(values)
}

cat(sum_all(1, 2, 3), "\n")
cat(sum_all(10, 20, 30, 40), "\n")
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
  1. Trying to read ... like a normal variable, as in length(...), instead of list(...) or c(...).
  2. Passing named arguments in ... and losing the names when using c(...).
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.