Returning from a Function
In this lesson, we will learn how to return a value from a function in R.
The return() Function
Let’s take an example: finding the maximum number between two numbers.
maxNumber <- function(myNumber1, myNumber2)
{
if(myNumber1 > myNumber2)
{
print(myNumber1)
} else
{
print(myNumber2)
}
}
Here, we are printing the maximum number. Now, suppose instead of printing the maximum number our program needs to perform some task with the maximum number. Therefore, instead of printing, we want the function maxNumber to give us the number, i.e., return the maximum number so that we can use it.
This is where the function return() comes in handy.
Syntax of return() Function
return(expression)
The value returned from a
functioncan be any valid object.
We can simply return the maximum number and the program calling the function can receive it.
In the above code snippet, the function maxNumber() uses return() to return the larger of the two numbers. The calling code, or driver code, receives this value and stores it in the max variable.
Returning a Value Without the return() Function
However, the interesting thing about the R language is; we need not specifically call the return() function. If there are no explicit returns from a function, the value of the last evaluated expression is returned automatically in R language.
Let’s try this out:
In the above code snippet, Line 5 is the last expression to be evaluated if the conditional statement myNumber1 > myNumber2 is satisfied. Therefore, this expression myNumber1 is simply returned. Similarly, Line 9 is the last line to be executed if the conditional statement myNumber1 > myNumber2 is NOT satisfied. Therefore, this expression myNumber2 is simply returned.
Processing Lists and Vectors
Let’s begin by modifying our example: Instead of two arguments, our function will now take argument which is a vector containing numbers and finds the maximum number amongst all of them.
Now, since we have a
vector, and we are going to iterate over each of its elements, we can use aloop.
In the above code, we used a reserved word:
-Inf
Reserved words are the set of words that have special meaning and purpose. These keywords cannot be used as identifiers.
Here, -Inf is used so that initially the variable max has the least value possible. Now, when the loop from Line 7 will start, the conditional statement max < v will evaluate to TRUE for the first element in any vector, unless, of course, if the first element is -Inf itself. This value will be stored in max.
From the second iteration onwards, the max variable is compared with the current element v. If the conditional statement max < v is satisfied, variable max is updated, otherwise, there is no change to max.
Returning Multiple Values in R
How about we introduce another modification.
Now, we want our function to return the maximum as well as the minimum value.
For this, we can store both the maximum and minimum value in a list and then return it. Let’s see how this is implemented:
In this code on Line 24 we return a list and therefore when we receive the output, we have to treat it like a list.
The variable
outputin this code snippet is a list with the first element the maximum value and the second element the minimum value.