Challenge - 5 Problems
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple function call
What is the output of the following R code?
R Programming
multiply_by_two <- function(x) {
return(x * 2)
}
result <- multiply_by_two(5)
print(result)Attempts:
2 left
💡 Hint
Think about what the function does to the input number.
✗ Incorrect
The function multiply_by_two takes an input x and returns x multiplied by 2. So multiply_by_two(5) returns 10.
❓ Predict Output
intermediate2:00remaining
Function with default argument
What will be printed when this R code runs?
R Programming
greet <- function(name = "Guest") { print(paste("Hello," , name)) } greet() greet("Alice")
Attempts:
2 left
💡 Hint
Check what happens when no argument is given.
✗ Incorrect
The function greet uses a default value 'Guest' for the parameter name. Calling greet() prints 'Hello, Guest'. Calling greet("Alice") prints 'Hello, Alice'.
❓ Predict Output
advanced2:00remaining
Function with variable scope
What is the output of this R code?
R Programming
x <- 10 add_to_x <- function(y) { x <- 5 return(x + y) } result <- add_to_x(3) print(result) print(x)
Attempts:
2 left
💡 Hint
Remember that variables inside functions have local scope.
✗ Incorrect
Inside the function, x is set to 5 locally. So add_to_x(3) returns 5 + 3 = 8. The global x remains 10, so print(x) outputs 10.
❓ Predict Output
advanced2:00remaining
Function with missing return statement
What will this R code print?
R Programming
sum_values <- function(a, b) {
a + b
}
result <- sum_values(4, 6)
print(result)Attempts:
2 left
💡 Hint
In R, the last expression is returned even without return().
✗ Incorrect
In R, if there is no explicit return(), the last evaluated expression is returned. So sum_values(4, 6) returns 10.
🧠 Conceptual
expert2:00remaining
Error type from incorrect function call
What error will this R code produce when run?
R Programming
my_function <- function(x, y) {
return(x + y)
}
my_function(5)Attempts:
2 left
💡 Hint
Check if all required arguments are provided.
✗ Incorrect
The function requires two arguments x and y. Calling it with only one argument causes an error about missing argument y.