Challenge - 5 Problems
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R function call?
Consider the following R function and its call. What value does it return?
R Programming
multiply_and_add <- function(x, y) {
result <- x * y
return(result + 5)
}
multiply_and_add(3, 4)Attempts:
2 left
💡 Hint
Remember the function multiplies x and y first, then adds 5 before returning.
✗ Incorrect
The function multiplies 3 and 4 to get 12, then adds 5, so the return value is 17.
❓ Predict Output
intermediate2:00remaining
What does this R function return when called?
Look at this R function and its call. What is the returned value?
R Programming
check_even <- function(num) {
if (num %% 2 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
check_even(7)Attempts:
2 left
💡 Hint
The function returns TRUE if the number is even, otherwise FALSE.
✗ Incorrect
7 is not even, so the function returns FALSE.
❓ Predict Output
advanced2:00remaining
What is the output of this nested return function?
Analyze the following R code. What value does the function return when called with input 5?
R Programming
nested_return <- function(x) {
inner <- function(y) {
return(y * 2)
}
return(inner(x) + 3)
}
nested_return(5)Attempts:
2 left
💡 Hint
The inner function doubles the input, then the outer adds 3.
✗ Incorrect
inner(5) returns 10, then adding 3 gives 13.
❓ Predict Output
advanced2:00remaining
What does this function return when no explicit return is used?
What is the output of this R function call?
R Programming
no_return <- function(x) {
y <- x + 2
y * 3
}
no_return(4)Attempts:
2 left
💡 Hint
In R, the last evaluated expression is returned if no return() is used.
✗ Incorrect
x=4, y=6, last expression y*3 = 18 is returned.
❓ Predict Output
expert3:00remaining
What is the output of this recursive function call?
Consider this recursive R function. What value does factorial(4) return?
R Programming
factorial <- function(n) {
if (n == 1) {
return(1)
} else {
return(n * factorial(n - 1))
}
}
factorial(4)Attempts:
2 left
💡 Hint
Factorial multiplies n by factorial of n-1 until n is 1.
✗ Incorrect
factorial(4) = 4 * 3 * 2 * 1 = 24.