What if your functions could guess the usual answers for you, so you only type what really matters?
Why Default argument values in R Programming? - Purpose & Use Cases
Imagine you write a function that calculates the area of a rectangle. Every time you call it, you have to type both the length and width, even if the width is usually 10. This means lots of repeated typing and mistakes if you forget the usual value.
Manually typing all arguments every time is slow and tiring. It's easy to make errors by forgetting to include common values or typing the wrong number. This wastes time and causes bugs that are hard to find.
Default argument values let you set common values once inside the function. When you call the function, you can skip those arguments if you want the default. This saves time, reduces errors, and makes your code cleaner.
area <- function(length, width) {
length * width
}
area(5, 10)area <- function(length, width = 10) { length * width } area(5)
Default argument values let your functions be flexible and easy to use, adapting to common cases without extra typing.
Think of a coffee machine where you usually want a medium cup. Instead of choosing size every time, the machine uses medium by default, but you can still pick small or large if you want.
Default values save you from typing the same arguments repeatedly.
They reduce mistakes by providing common values automatically.
They make your functions simpler and more flexible to use.