Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 26 / 68

Default arguments

This lesson covers default argument values in R, with the aim of writing flexible functions whose arguments can be left out.

Writing = value on a parameter sets the initial value used when the caller leaves that argument out. Picture an order form where leaving a box blank gets you the usual set.

The sample code defines greet <- function(name = "Guest"), which handles both greet("Alice") with a name and greet() with nothing at all.

A common early stumble is the sep argument of cat(). R's cat() separates with a space by default, so to control the separator you have to say sep = "" explicitly.

In professional work, letting the caller omit arguments produces flexible functions that serve both simple use and fine-grained control.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)