+5 votes
in Programming Languages by (40.7k points)

When I try to return multiple values from a function using return(), it gives an error: "multi-argument returns are not permitted". How can I return multiple values?

1 Answer

+1 vote
by (157k points)

R will give the error if you try to return values separately [e.g., return(x,y)]. Instead, you can return values as a vector, and then you can access the returned values using their indices.

Here is an example to show how to do it:

Function definition:

sqr_sqrt <- function(x){
  return(c(x^2, sqrt(x)))

Call the function and access the returned values.

n <- 16
v <- sqr_sqrt(16)