0
0
R Programmingprogramming~20 mins

Function arguments in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Arguments Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function with default and named arguments
What is the output of this R code?
R Programming
f <- function(x, y = 2) {
  x + y
}

result <- f(y = 3, x = 4)
print(result)
AError: unused argument (y = 3)
B[1] 5
C[1] 7
D[1] 6
Attempts:
2 left
💡 Hint
Remember that named arguments can be passed in any order.
Predict Output
intermediate
2:00remaining
Effect of missing argument with default
What will this R code print?
R Programming
g <- function(a, b = 10) {
  a * b
}

print(g(5))
A[1] 50
BError: argument "b" is missing, with no default
C[1] 15
D[1] 5
Attempts:
2 left
💡 Hint
Check the default value of b.
🔧 Debug
advanced
2:00remaining
Identify the error in function argument usage
What error does this R code produce?
R Programming
h <- function(x, y) {
  x + y
}

h(3)
AError in h(3) : unused argument (3)
BError in h(3) : argument "y" is missing, with no default
CError in h(3) : object 'y' not found
DNo error, output is 3
Attempts:
2 left
💡 Hint
Check if all required arguments are provided.
Predict Output
advanced
2:00remaining
Output with ... (ellipsis) argument
What does this R code print?
R Programming
k <- function(x, ...) {
  args <- list(...)
  sum <- x
  for (val in args) {
    sum <- sum + val
  }
  sum
}

print(k(1, 2, 3, 4))
A[1] 10
B[1] 24
C[1] 1
DError: object 'val' not found
Attempts:
2 left
💡 Hint
The ... collects extra arguments as a list.
🧠 Conceptual
expert
2:00remaining
Number of arguments in a function call
How many arguments are passed to the function call below?
R Programming
m <- function(a, b = 2, c = 3) {
  a + b + c
}

m(1, c = 4)
AError: missing argument
B3
C1
D2
Attempts:
2 left
💡 Hint
Count only arguments explicitly passed in the call.