01 / 21
f(x)

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.

R Programming

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.

greet <- function(name, greeting) { paste(greeting, name) } greet("Alice", "Hello") # [1] "Hello Alice"

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.

Default Arguments
Provide a fallback value so callers can omit common inputs
Named Arguments
Let callers specify arguments by name, ignoring position order

Two Ways R Matches Arguments

Positional Matching Order matters f("A", "B", 10) 1st: x 2nd: y 3rd: z 1st arg → 1st param, etc. Named Matching Order doesn't matter f(z=10, x="A", y="B") x y z Name matches param, any order

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.

power <- function(base, exponent = 2) { base ^ exponent } power(5) # exponent defaults to 2 → 25 power(5, 3) # exponent overridden → 125

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.

substring("Hello World", first = 7, last = 11) # [1] "World" # Same result, different order: substring(last = 11, first = 7, "Hello World") # [1] "World"

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

1. Match exact names 2. Match partial names 3. Match by position 4. Use defaults for unmatched 5. Error if still missing

How to Apply These Concepts

1
Identify required vs optional parameters. Which inputs must the caller always provide? Which have sensible fallbacks?
2
Set defaults for optional ones. Use param = value in the function signature. Place required params first, optional ones after.
3
Call with named arguments when skipping middle parameters, when the function has many arguments, or when clarity matters more than brevity.
4
Mix positional and named. Use positional for the first 1-2 obvious args, then name the rest. This is standard R style.

Example 1: Simple Defaults

format_price <- function(amount, currency = "$", decimals = 2) { paste0(currency, format(round(amount, decimals), nsmall = decimals)) } format_price(19.5) # [1] "$19.50" (both defaults used) format_price(19.5, "€") # [1] "€19.50" (currency overridden, decimals default) format_price(19.5, decimals = 0) # [1] "$20" (named arg skips currency)

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.

make_range <- function(start, end, step = (end - start) / 10) { seq(start, end, by = step) } make_range(0, 100) # step defaults to (100 - 0) / 10 = 10 # [1] 0 10 20 30 40 50 60 70 80 90 100 make_range(0, 100, step = 25) # [1] 0 25 50 75 100

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():

# Hard to read: what do TRUE and "," mean? read.csv("data.csv", TRUE, ",", ".") # Clear: every argument is self-documenting read.csv( file = "data.csv", header = TRUE, sep = ",", dec = "." )

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().

describe <- function(x, label = NULL) { if (is.null(label)) { label <- deparse(substitute(x)) } cat(label, ": mean =", mean(x), "\n") } describe(1:10) # 1:10 : mean = 5.5 describe(1:10, label = "My Data") # My Data : mean = 5.5

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.

my_plot <- function(x, y, title = "Plot", ...) { plot(x, y, main = title, ...) } # col and pch are passed through ... to plot() my_plot(1:5, 1:5, col = "red", pch = 19)

Common Mistakes to Avoid

✕ Wrong Order Without Names
Passing f(10, "A") when the function expects f(text, number). Always name args when skipping or reordering.
✕ Using = Instead of <-
Inside a function call, = means named argument. Using <- there creates a variable in the parent scope instead!
✕ Forgetting Required Args
Assuming every argument has a default. If a required arg is missing, R throws: argument "x" is missing, with no default.

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?