R Function Arguments
Arguments are the inputs you pass into a function. R gives you several flexible ways to define and pass arguments: required arguments, default values, named arguments, and variable-length argument lists. Understanding these options lets you write functions that are both powerful and easy to use.
Required Arguments
power <- function(base, exponent) {
return(base ^ exponent)
}
power(2, 10) # 1024
power(3, 3) # 27
# power(5) # ERROR: exponent is missing
Default Argument Values
Provide a default so the argument becomes optional. The caller can override it or leave it at the default.
greet <- function(name, greeting = "Hello") {
cat(greeting, ",", name, "!\n")
}
greet("Priya") # Hello , Priya ! (uses default)
greet("Arjun", "Namaste") # Namaste , Arjun ! (overrides default)
Named Arguments
When calling a function, name the arguments to pass them in any order.
divide <- function(numerator, denominator) {
return(numerator / denominator)
}
divide(10, 2) # 5 (positional)
divide(numerator = 10, denominator = 2) # 5 (named)
divide(denominator = 2, numerator = 10) # 5 (named, any order)
Argument Matching Diagram
Function: power(base, exponent) Call: power(exponent = 3, base = 2) "base" ← 2 "exponent" ← 3 Result: 2^3 = 8
Variable Number of Arguments: ...
The ... (dots or ellipsis) argument accepts any number of extra values. This is how functions like sum() and paste() accept unlimited inputs.
my_sum <- function(...) {
values <- c(...)
return(sum(values))
}
my_sum(1, 2, 3) # 6
my_sum(10, 20, 30, 40) # 100
with_message <- function(msg, ...) {
cat(msg, "\n")
cat(...) # pass extra args to cat()
}
with_message("Numbers:", 1, 2, 3, sep="-")
# Numbers:
# 1-2-3
Checking Arguments Inside a Function
safe_sqrt <- function(x) {
if (!is.numeric(x)) stop("x must be numeric")
if (x < 0) stop("x must be non-negative")
return(sqrt(x))
}
safe_sqrt(16) # 4
safe_sqrt(-4) # Error: x must be non-negative
safe_sqrt("hi") # Error: x must be numeric
Missing Arguments
describe <- function(name, age) {
if (missing(age)) {
cat(name, "(age unknown)\n")
} else {
cat(name, "is", age, "years old\n")
}
}
describe("Riya") # Riya (age unknown)
describe("Riya", 28) # Riya is 28 years old
match.arg() — Constrain to Valid Choices
plot_type <- function(type = c("bar", "line", "scatter")) {
type <- match.arg(type) # validates and completes partial matches
cat("Plotting:", type, "\n")
}
plot_type("bar") # Plotting: bar
plot_type("l") # Plotting: line (partial match works!)
plot_type("pie") # Error: 'arg' should be one of 'bar', 'line', 'scatter'
Practical: Flexible Report Generator
generate_report <- function(data,
title = "Data Summary",
digits = 2,
verbose = FALSE) {
cat("=====", title, "=====\n")
cat("Mean: ", round(mean(data), digits), "\n")
cat("Median:", round(median(data), digits), "\n")
if (verbose) {
cat("SD: ", round(sd(data), digits), "\n")
cat("Range: ", min(data), "to", max(data), "\n")
}
}
scores <- c(78, 85, 92, 70, 88)
generate_report(scores) # minimal output
generate_report(scores, title="Scores", verbose=TRUE) # full output
Well-designed function arguments make your code flexible without making it complicated. Default values cover the common case, required arguments enforce what is truly needed, and ... handles open-ended inputs. These tools let you write functions that are both easy to use for beginners and powerful enough for advanced users.
