A return value is the result that a function sends back after it finishes running. It is what you get when you call the function.
Click to reveal answer
beginner
How do you explicitly return a value from a function in R?
You use the return() function inside your function to send back a specific value.
Click to reveal answer
beginner
What happens if you do not use return() in an R function?
R automatically returns the last evaluated expression in the function, even if you don't use return().
Click to reveal answer
intermediate
Can a function in R return multiple values?
Yes, by returning a list or other data structure, a function can return multiple values at once.
Click to reveal answer
beginner
What is the difference between print() and returning a value in R?
print() shows output on the screen but does not send a value back from the function. Returning a value allows other code to use that result.
Click to reveal answer
What does the return() function do in R?
ASends a value back from a function
BPrints a value to the console
CStops the entire program
DCreates a new variable
✗ Incorrect
return() sends a value back from a function to where it was called.
If a function in R does not have a return() statement, what happens?
AThe function returns the last evaluated expression
BThe function returns NULL
CThe function causes an error
DThe function returns TRUE
✗ Incorrect
R automatically returns the last evaluated expression if return() is not used.
How can a function return multiple values in R?
ABy using global variables
BBy using multiple <code>return()</code> statements
CBy printing all values
DBy returning a list containing all values
✗ Incorrect
Returning a list lets a function send back multiple values together.
Which of these is NOT true about return values in R?
AReturn values can be used in other parts of the program
BReturn values are always printed automatically
CReturn values come from functions
DReturn values can be any type of data
✗ Incorrect
Return values are not printed automatically; you must print them if you want to see them.
What is the main difference between print() and returning a value?
A<code>print()</code> sends a value back, return shows output
BThey do the same thing
C<code>print()</code> shows output, return sends a value back
DNeither can be used in functions
✗ Incorrect
print() displays output on screen; returning sends a value back for further use.
Explain what a return value is in R and how it is used in functions.
Think about what happens after a function finishes running.
You got /3 concepts.
Describe how you can return multiple values from a single R function.
Consider how to package several things together.
You got /3 concepts.
Practice
(1/5)
1. What does the return() function do in an R function?
easy
A. It sends a value back to where the function was called.
B. It prints a message on the screen.
C. It stops the entire program immediately.
D. It creates a new variable inside the function.
Solution
Step 1: Understand the purpose of return()
The return() function is used inside an R function to send a value back to the place where the function was called.
Step 2: Differentiate from other actions
Printing messages or stopping the program are different actions and not the role of return().
Final Answer:
It sends a value back to where the function was called. -> Option A
Quick Check:
Return sends value back = A [OK]
Hint: Return sends output back from function to caller [OK]
Common Mistakes:
Thinking return prints output
Confusing return with stopping program
Assuming return creates variables
2. Which of the following is the correct way to return the value 10 from a function in R?
easy
A. return 10
B. return(10)
C. return{10}
D. return <- 10
Solution
Step 1: Recall correct return syntax in R
In R, the correct syntax to return a value is to use return() with parentheses around the value.
Step 2: Check each option
return(10) uses return(10), which is correct. Others have syntax errors or wrong usage.
Final Answer:
return(10) -> Option B
Quick Check:
Use parentheses with return = A [OK]
Hint: Use parentheses with return to send value back [OK]
Common Mistakes:
Omitting parentheses after return
Using curly braces instead of parentheses
Assigning return as a variable
3. What will be the output of this R code?
my_func <- function() {
x <- 5
y <- 10
x + y
}
result <- my_func()
print(result)
medium
A. NULL
B. 5
C. Error
D. 15
Solution
Step 1: Understand function return behavior without explicit return()
In R, if no return() is used, the last evaluated expression is returned automatically. Here, x + y is the last expression.
Step 2: Calculate the last expression value
x = 5 and y = 10, so x + y equals 15. This value is returned and stored in result.
Final Answer:
15 -> Option D
Quick Check:
Last expression returned = 15 [OK]
Hint: Last expression in function returns automatically if no return() [OK]
Common Mistakes:
Expecting NULL if no return()
Thinking print inside function returns value
Confusing variable assignment with return
4. Identify the error in this R function that tries to return the sum of two numbers:
add_numbers <- function(a, b) {
sum <- a + b
return sum
}
medium
A. Variable sum is not defined
B. Function should not use return
C. Missing parentheses after return
D. Function parameters are incorrect
Solution
Step 1: Check return statement syntax
The return statement in R requires parentheses around the value to return. Here, return sum is missing parentheses.
Step 2: Confirm variable and parameters are correct
The variable sum is defined as a + b, and parameters are correct. So no error there.
Final Answer:
Missing parentheses after return -> Option C
Quick Check:
return() needs parentheses = B [OK]
Hint: Always use parentheses with return in R [OK]
Common Mistakes:
Omitting parentheses after return
Confusing variable scope errors
Thinking return is optional always
5. You want to write an R function that returns a named list with two values: the sum and product of two numbers. Which code correctly returns both values?
hard
A. calc <- function(a, b) {
return(list(sum = a + b, product = a * b))
}
B. calc <- function(a, b) {
sum <- a + b
product <- a * b
return(sum, product)
}
C. calc <- function(a, b) {
sum <- a + b
product <- a * b
return c(sum, product)
}
D. calc <- function(a, b) {
sum <- a + b
product <- a * b
list(sum, product)
}
Solution
Step 1: Understand how to return multiple values in R
R functions can return a list to send back multiple values together.
Step 2: Check each option for correct syntax
calc <- function(a, b) {
return(list(sum = a + b, product = a * b))
} correctly returns a named list with sum and product. calc <- function(a, b) {
sum <- a + b
product <- a * b
return(sum, product)
} tries to return two values separately, which is invalid. calc <- function(a, b) {
sum <- a + b
product <- a * b
return c(sum, product)
} returns a vector instead of a list. calc <- function(a, b) {
sum <- a + b
product <- a * b
list(sum, product)
} returns a list but without return(), so it returns the last expression implicitly, which works but does not name elements.
Final Answer:
calc <- function(a, b) {
return(list(sum = a + b, product = a * b))
} -> Option A
Quick Check:
Return named list for multiple values = C [OK]
Hint: Use return(list(...)) to send multiple values back [OK]
Common Mistakes:
Trying to return multiple values without list
Using return with multiple arguments incorrectly
Forgetting to name list elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.