A **function** in R can be used to perform some specific task.

When creating your own custom function in R, you may want to **exit** the function if some condition is not met.

There are two common ways to do so:

**Method 1: Use stop() to Exit Function**

**cube_number = function(x) {
if (x > 1) {
print("Condition Met")
} else if (x <= 1) {
stop("Condition Not Met")
}
return(x**3)
}
**

Here is what this function does:

- First, it checks if the number provided to the function is greater than 1.
- If the number is greater than 1 then the function returns “Condition Met” and then returns the number cubed.
- If the number is not greater than 1 then the function returns an error message with the specific text “Condition Not Met.”

Using this method, we exit the function if the number provided is not greater than 1 by using the **stop()** function.

**Method 2: Use return() to Exit Function**

**cube_number = function(x) {
if (x > 1) {
print("Condition Met")
} else if (x <= 1) {
return(NA)
}
return(x**3)
}
**

Here is what this function does:

- First, it checks if the number provided to the function is greater than 1.
- If the number is greater than 1 then the function returns “Condition Met” and then returns the number cubed.
- If the number is not greater than 1 then the function returns NA as the result.

Using this method, we exit the function if the number provided is not greater than 1 by using the **return()** function to simply return NA.

The following examples show how to use each method in practice.

**Example 1: Exit a Function in R Using stop()**

We can create the following function named **cube_number()** to cube the number provided to the function:

**cube_number = function(x) {
if (x > 1) {
print("Condition Met")
} else if (x <= 1) {
stop("Condition Not Met")
}
return(x**3)
}**

Suppose we run this function using **5** as the argument:

**cube_number(5)
[1] "Condition Met"
[1] 125**

The function returns “Condition Met” along with the value **125**, which represents the number 5 cubed.

Suppose instead we run this function using **1** as the argument:

**cube_number(1)
Error in cube_number(1) : Condition Not Met
**

The function returns an error message and states “Condition Not Met” since the number we provided is not greater than 1.

**Example 2: Exit a Function in R Using return()**

We can create the following function named **cube_number()** to cube the number provided to the function:

**cube_number = function(x) {
if (x > 1) {
print("Condition Met")
} else if (x <= 1) {
return(NA)
}
return(x**3)
}**

Suppose we run this function using **5** as the argument:

**cube_number(5)
[1] "Condition Met"
[1] 125**

The function returns “Condition Met” along with the value **125**, which represents the number 5 cubed. This matches the value from the previous example.

Suppose instead we run this function using **0** as the argument:

**cube_number(0)
[1] NA
**

The function returns the value NA since the number we provided is not greater than 1.

Note that you can also provide a different value besides NA if you’d like within the **return()** function.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Write a Repeat Loop in R

How to Append Values to a Vector Using a Loop in R

How to Create a Nested For Loop in R

How to Use While Loops in R