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

Default Arguments

In this page:

  1. Default Arguments

Default Arguments

A parameter can have a default value specified in the function definition, making it optional at the call site. If the caller doesn't supply that argument, the default is used instead. Default values can even reference other parameters of the same function.

Note: Place parameters with defaults after required parameters, mirroring the convention used in most other languages.

Example: Default Arguments

markup
greet <- function(name, greeting = "Hello") {
  cat(greeting, ",", name, "!\n")
}

greet("Alice")
greet("Bob", "Welcome")
🔒

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.