Default & Named
Arguments in R
Master how to write flexible functions using default values and named arguments to make your R code cleaner, safer, and more readable.
What Are Function Arguments?
When you call a function in R, you pass arguments: the values the function needs to do its job. Arguments are the inputs listed inside the parentheses of a function definition.
Why Defaults & Named Args Matter
As functions grow more complex with many parameters, two problems arise: callers must remember every argument's position and provide every value even when most are standard. Defaults and named arguments solve both.
Two Ways R Matches Arguments
Default Arguments: The Core Rule
A default argument is a value assigned to a parameter in the function definition. If the caller omits that argument, R automatically uses the default.
Named Arguments: Call by Name
A named argument is specified at call time using the syntax param_name = value. This lets you supply arguments in any order and skip positional guessing.
Definition-Time vs Call-Time
| Default Arguments | Named Arguments | |
|---|---|---|
| Where used | Function definition | Function call |
| Syntax | param = value in signature | param = value in call |
| Purpose | Provide fallback values | Specify which param gets which value |
| Order matters? | N/A (set once) | No: any order is valid |
| Who benefits | Function author | Function caller |
How R Resolves Arguments
How to Apply These Concepts
Example 1: Simple Defaults
Example 2: Dynamic Defaults
In R, a default value can be an expression that references other parameters. The default is evaluated at call time, after the referenced argument is known.
Example 3: Named Args in Practice
Real R functions often have many parameters. Named arguments keep your code readable. Consider the built-in read.csv():
Using NULL as a Default
Sometimes a parameter should be truly optional: no fallback value makes sense. In R, use NULL as the default and check with is.null().
The ... (Dots) Argument
R has a special argument ... (called "dots" or "ellipsis") that captures any number of extra named or unnamed arguments and passes them to inner functions.
Common Mistakes to Avoid
Summary Cheat Sheet
| Concept | Syntax | Example |
|---|---|---|
| Default argument | param = value in definition | function(x, n = 5) |
| Named argument | param = value in call | f(n = 10, x = 3) |
| NULL default | param = NULL | function(x, label = NULL) |
| Dots passthrough | ... in signature | function(x, ...) |
| Positional call | Values in order | f(3, 5) |
| Mixed call | Positional + named | f(3, n = 5) |
Question 1 of 5
Given the function:
calc <- function(a, b = 10, c = 3) { a + b * c }
What does calc(2) return?
Question 2 of 5
Given:
f <- function(x, y = 5, z = 1) { x * y + z }
What does f(3, z = 10) return?
Question 3 of 5
Which statement about default arguments in R is true?
Question 4 of 5
What happens if you write f(x <- 5) instead of f(x = 5)?
Question 5 of 5
Given:
g <- function(a, ..., sep = "-") { paste(a, ..., sep = sep) }
What does g("x", "y", "z") return?