+5 votes
in Programming Languages by (71.8k 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

+2 votes
by (349k points)
selected by
 
Best answer

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)
print(v[1])
print(v[2])


...