Challenge - 5 Problems
Default Argument Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with default argument
What is the output of this R code when calling
greet()?R Programming
greet <- function(name = "friend") { paste("Hello", name) } greet()
Attempts:
2 left
💡 Hint
Check what value the function uses when no argument is given.
✗ Incorrect
The function greet has a default argument 'name' set to "friend". Calling greet() uses this default and returns "Hello friend".
❓ Predict Output
intermediate2:00remaining
Effect of missing argument with default
What will be the output of this R code?
R Programming
multiply <- function(x, y = 2) { x * y } multiply(5)
Attempts:
2 left
💡 Hint
Look at the default value of y and how multiply is called.
✗ Incorrect
The function multiply uses y=2 by default. Calling multiply(5) means x=5 and y=2, so 5*2=10.
🔧 Debug
advanced2:00remaining
Identify the error in default argument usage
What error does this R code produce when run?
R Programming
f <- function(x = y, y = 3) {
x + y
}
f()Attempts:
2 left
💡 Hint
Default arguments are evaluated when the function is called, in order.
✗ Incorrect
The default value for x is y, but y is not defined before x's default is evaluated, causing an error.
❓ Predict Output
advanced2:00remaining
Output with default argument as a function call
What is the output of this R code?
R Programming
get_default <- function() {
7
}
add <- function(x, y = get_default()) {
x + y
}
add(3)Attempts:
2 left
💡 Hint
Default arguments can be set by calling other functions.
✗ Incorrect
The default value for y is the result of get_default(), which returns 7. So add(3) returns 3+7=10.
🧠 Conceptual
expert3:00remaining
Understanding evaluation timing of default arguments
Consider this R code. What is the value of
result after running it?R Programming
x <- 5 f <- function(y = x) { x <- 10 y } result <- f()
Attempts:
2 left
💡 Hint
Default arguments are evaluated when the function is called, before the function body runs.
✗ Incorrect
The default argument y = x uses the global x = 5 at call time. The local x = 10 inside the function does not affect y's default.