In this article, you’ll learn how to return a value from a function in R language. You’ll also discover how to employ functions that don’t have a return function. We will frequently need our functions to perform some processing and provide the output. This is performed in R language by using the return() function.
To better understand this function, follow the article below.
The syntax of return() function in R
The Syntax: return one a value
functionName <- function(arguments) { # Body code return(expression) } functionName(val)
- functionName: The name of the function.
- arguments: The parameters are passed.
- expression: The value or any valid objects can be returned by a function.
- functionName(val): Return the value pass values to the arguments.
The Syntax: return multiples value as a list
functionName <- function(arguments) { # Body code return(list(val_1,val_2,.,val_n)) } functionName(val)
- functionName: The name of the function.
- arguments: The parameters are passed.
- expression: The value or any valid objects can be returned by a function.
- functionName(val): Return the value pass values to the arguments.
The example of return() function in R
Here, we will refer to 3 examples: return a value, multiple values, and without value.
Return one value
# Function with return one value mean <- function(num1,num2){ # Calculate the mean value m <- (num1 + num2) / 2 # Return the result return(m) } mean(4,5) mean(6,8)
Output
[1] 4.5
[1] 7
The function only returns one result: the product of 4 and 5 or 6 and 8.
Return multiple values as a list
The code below demonstrates how to write a function that returns multiple values:
# Function with multiple values calc <- function(x, y, z){ # Addition addition = x + y + z # Multiplication mulp = x * y * z return(list(addition,mulp)) } calc(5,8,6)
Output
[[1]]
[1] 19
[[2]]
[1] 240
Note: We returned two values in this example, but you may use a similar syntax to return as many values as you like with the return() option.
Return a value without using return
# Function with multiple values calc <- function(x, y, z){ # Addition addition = x + y + z # Return result of the function addition } calc(5,8,6)
Output
[1] 19
Summary
This article will show you how to use the return() function in R language. I hope you find the information in this post beneficial. If you have any troubles, please post a comment. I’ll react. Thank you for reading, and best wishes!
Maybe you are interested:
- The slice() function in R: How to use slice() in R
- write() Function In R: The Usage And The Example
- The Dist() Function In R: How to use dist() in R

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript