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.

**Example:**

`# 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` `) ` |

*chevron_right*

*filter_none*

**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 can have multiple arguments too. Below is an implementation of a function with multiple arguments.

**Example:**

`# 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` `) ` |

*chevron_right*

*filter_none*

**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

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 function with default value.

**Example:**

`# 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` `) ` |

*chevron_right*

*filter_none*

**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.

**Example:**

`# Function definition of dots operator ` `fun <` `-` `function(n, ...){ ` ` ` `l <` `-` `list` `(n, ...) ` ` ` `paste(l, collapse ` `=` `" "` `) ` `} ` ` ` `# Function call ` `fun(` `5` `, ` `1L` `, ` `6i` `, TRUE, ` `"GeeksForGeeks"` `, ` `"Dots operator"` `) ` |

*chevron_right*

*filter_none*

**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.

**Example:**

`# 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)` |

*chevron_right*

*filter_none*

**Output:**

[1] 55 [1] 0.2153183

## Recommended Posts:

- Get the List of Arguments of a Function in R Programming - args() Function
- Get Addition of the Objects passed as Arguments in R Programming - sum() Function
- Combine Arguments into a Vector in R Programming - c() Function
- Getting Multiplication of the Objects passed as Arguments in R Language - prod() Function
- How to create optional arguments for a SASS mixin?
- Scala | Named Arguments
- How to describe “object” arguments in jsdoc?
- How to convert arguments object into an array in JavaScript ?
- Difference between multiple arguments and options object
- Golang program that uses func with two arguments
- Command Line Arguments in Golang
- Check if a Function is a Primitive Function in R Programming - is.primitive() Function
- Compute Density of the Distribution Function in R Programming - dunif() Function
- Apply a Function over a List of elements in R Programming - lapply() Function
- Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
- Compute the Natural Logarithm of the Absolute Value of Gamma Function in R Programming - lgamma() Function
- Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function
- Compute the Second Derivative of the Logarithmic value of the gamma Function in R Programming - trigamma() Function
- Recursively apply a Function to a List in R Programming - rapply() function
- Apply a Function over a Ragged Array in R Programming - tapply() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.