0
0
R Programmingprogramming~20 mins

Default argument values in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Argument 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 argument
What is the output of this R code when calling greet()?
R Programming
greet <- function(name = "friend") {
  paste("Hello", name)
}
greet()
A"Hello friend"
B"Hello name"
CError: object 'name' not found
D"Hello"
Attempts:
2 left
💡 Hint
Check what value the function uses when no argument is given.
Predict Output
intermediate
2: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)
AError: argument 'y' missing
B10
C5
DNULL
Attempts:
2 left
💡 Hint
Look at the default value of y and how multiply is called.
🔧 Debug
advanced
2: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()
AError: argument 'x' missing
B3
C6
DError: object 'y' not found
Attempts:
2 left
💡 Hint
Default arguments are evaluated when the function is called, in order.
Predict Output
advanced
2: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)
A10
B3
CError: object 'get_default' not found
DNULL
Attempts:
2 left
💡 Hint
Default arguments can be set by calling other functions.
🧠 Conceptual
expert
3: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()
AError: object 'x' not found
B10
C5
DNULL
Attempts:
2 left
💡 Hint
Default arguments are evaluated when the function is called, before the function body runs.