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

Function Arguments

In this page:

  1. Function Arguments

Function Arguments

Arguments are declared in the function's parameter list and can be passed either positionally or by name at the call site. Named arguments can be given in any order, which is especially useful for functions with many parameters. Mixing positional and named arguments is allowed, as long as positional ones come first.

Note: Calling with named arguments, like f(name = "Sam", times = 3), is more readable and order-independent.

Example: Function Arguments

markup
greet <- function(name, times) {
  for (i in 1:times) {
    cat("Hello,", name, "!\n")
  }
}

greet("Sam", 2)
greet(times = 1, name = "Priya")
🔒

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.