Open In App

Function Arguments in R Programming

Last Updated : 24 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Arguments are the parameters provided to a function to perform operations in a programming language. In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R. In this article, we’ll discuss different ways of adding arguments in a function in R programming.

Adding Arguments in R

We can pass an argument to a function while calling the function by simply giving the value as an argument inside the parenthesis. Below is an implementation of a function with a single argument.

Syntax:

function_name <- function(arg1, arg2, … )
 { 
code 
 }

Here is one example that explain Function Arguments in R Programming.

R
calculate_square <- function(x) {
  result <- x^2
  return(result)
}

value1 <- 5
square1 <- calculate_square(value1)
print(square1)  

value2 <- -2.5
square2 <- calculate_square(value2)
print(square2) 

Output:

[1] 25

[1] 6.25

Function to Check if a Number is Divisible by 5

Here’s a simple function in R that checks whether a given number is divisible by 5 or not. The function definition and calls are provided below.

R
# Function definition
# To check n is divisible by 5 or not
divisbleBy5 <- function(n){
if(n %% 5 == 0)
{
    return("number is divisible by 5")
}
else
{
    return("number is not divisible by 5")
}
}

# Function call
divisbleBy5(100)
divisbleBy5(4)
divisbleBy5(20.0)

Output:

[1] "number is divisible by 5"
[1] "number is not divisible by 5"
[1] "number is divisible by 5"

Adding Multiple Arguments in R

A function in R Programming Language can have multiple arguments too. Below is an implementation of a function with multiple arguments.

R
# Function definition
# To check a is divisible by b or not
divisible <- function(a, b){
if(a %% b == 0)
{
    return(paste(a, "is divisible by", b))
}
else
{
    return(paste(a, "is not divisible by", b))
}
}

# Function call
divisible(7, 3)
divisible(36, 6)
divisible(9, 2)

 
Output:

[1] "7 is not divisible by 3"
[1] "36 is divisible by 6"
[1] "9 is not divisible by 2"

Adding Default Value in R

The default value in a function is a value that is not required to specify each time the function is called. If the value is passed by the user, then the user-defined value is used by the function otherwise, the default value is used. Below is an implementation of a function with a default value.

R
# Function definition to check
# a is divisible by b or not.
# If b is not provided in function call,
# Then divisibility of a is checked with 3 as default
divisible <- function(a, b = 3){
if(a %% b == 0)
{
    return(paste(a, "is divisible by", b))
}
else
{
    return(paste(a, "is not divisible by", b))
}
}

# Function call
divisible(10, 5)
divisible(12)

Output:

[1] "10 is divisible by 5"
[1] "12 is divisible by 3"

Dots Argument

Dots argument (…) is also known as ellipsis which allows the function to take an undefined number of arguments. It allows the function to take an arbitrary number of arguments. Below is an example of a function with an arbitrary number of arguments.

R
# Function definition of dots operator
fun <- function(n, ...){
l <- list(n, ...)
paste(l, collapse = " ")
}

# Function call
fun(5, 1L, 6i, TRUE, "GeeksForGeeks", "Dots operator")

Output:

[1] "5 1 0+6i TRUE GeeksForGeeks Dots operator"

Function as Argument

In R programming, functions can be passed to another functions as arguments. Below is an implementation of function as an argument.

R
# Function definition
# Function is passed as argument
fun <- function(x, fun2){
return(fun2(x))
}

# sum is built-in function
fun(c(1:10), sum)

# mean is built-in function
fun(rnorm(50), mean)

Output:

[1] 55
[1] 0.2153183

Conclusion

Function arguments in R enhances the functionality and usability of your functions. It allows you to write more robust, versatile, and user-friendly code. Whether dealing with simple or complex functions, mastering function arguments is a foundational skill in R programming that contributes significantly to efficient coding practices.




Previous Article
Next Article

Similar Reads

Get the List of Arguments of a Function in R Programming - args() Function
args() function in R Language is used to get the required arguments by a function. It takes function name as arguments and returns the arguments that are required by that function. Syntax: args(name) Parameters: name: Function name Returns: For a closure: Formal Argument list but with NULL body For a Primitive Function: A closure with usage and a N
1 min read
Combine Arguments into a Vector in R Programming - c() Function
c() function in R Language is used to combine the arguments passed to it. Syntax: c(...) Parameters: ...: arguments to be combined Example 1: # R program to cumulative maxima # Calling c() function x &lt;- c(1, 2, 3, 4) x Output: [1] 1 2 3 4 Example 2: # R program to cumulative maxima # Calling c() function x &lt;- c(&quot;a&quot;, &quot;b&quot;,
1 min read
Getting Multiplication of the Objects passed as Arguments in R Language - prod() Function
prod() function in R Language is used to return the multiplication results of all the values present in its arguments. Syntax: prod(...) Parameters: ...: numeric or complex or logical vectors Example 1: # R program to illustrate # prod function # Initializing some vectors of element x &lt;- c(1, 2, 3, 4) y &lt;- c(0, 2, 4, 6) z &lt;- c(2.6, 3.7, 1.
1 min read
Specify Multiple Arguments in apply Functions in R
In this article, we will discuss how to specify multiple arguments in apply functions in the R programming language. apply() function is used to apply conditions to get the resultant data like mean of data, the sum of data, etc. Syntax: apply(data, margin, function, na.rm = TRUE) where, data is the input dataframemargin is used to specify the width
2 min read
How to Fix in R: Arguments imply differing number of rows
In this article, we are going to see how to fix Arguments that imply differing numbers of rows in R Programming Language. One error that we may encounter in R is: arguments imply differing number of rows: 6, 5 The compiler produces such an error when we try to create a data frame and the number of rows in each column of the data frame differs from
2 min read
How to Fix: Error in select unused arguments in R?
In this article, we will be looking toward the approach to fix the error in selecting unused arguments in the R programming language. Error in selecting unused arguments: The R compiler produces this error when a programmer tries to use the select() function of the dplyr package in R provided that the MASS package loaded. R tries to make use of the
2 min read
Check if a Function is a Primitive Function in R Programming - is.primitive() Function
is.primitive() function in R Language is used to check if a function is a primitive function, i.e. its either a built-in function or a special function. Syntax: is.primitive(func) Parameters: func: Function to be checked Example 1: # R program to illustrate # the use of is.primitive function # Calling is.primitive() function is.primitive(1) is.prim
1 min read
Compute Density of the Distribution Function in R Programming - dunif() Function
dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: # Create vector of random deviation u &lt;- runif(20)
1 min read
Apply a Function over a List of elements in R Programming - lapply() Function
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean
2 min read
Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
optimize() or optimise() function in R Language is used to search the interval from lower to upper for a minimum or maximum of the function f with respect to its first argument. Syntax: optimize(f, interval, maximum)Parameters: f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the va
2 min read
Article Tags :