Challenge - 5 Problems
Function Arguments Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that named arguments can be passed in any order.
✗ Incorrect
The function f adds x and y. The call f(y=3, x=4) assigns x=4 and y=3, so the result is 4 + 3 = 7. But the default y=2 is overridden by y=3. The sum is 7.
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Check the default value of b.
✗ Incorrect
The function multiplies a by b. Since b has a default value of 10 and is not provided, it uses 10. So 5 * 10 = 50.
🔧 Debug
advanced2: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)Attempts:
2 left
💡 Hint
Check if all required arguments are provided.
✗ Incorrect
The function h requires two arguments x and y. Calling h(3) provides only x, so R raises an error about missing argument y.
❓ Predict Output
advanced2: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))Attempts:
2 left
💡 Hint
The ... collects extra arguments as a list.
✗ Incorrect
The function sums x plus all values passed in ... . Here, x=1 and ... = 2,3,4. Sum is 1+2+3+4=10.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
Count only arguments explicitly passed in the call.
✗ Incorrect
The call m(1, c=4) passes two arguments: a=1 (positional) and c=4 (named). b uses default 2. So 2 arguments are passed.