VOOZH about

URL: https://www.geeksforgeeks.org/r-language/function-arguments-in-r-programming/

⇱ Function Arguments in R Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Function Arguments in R Programming

Last Updated : 12 Jul, 2025

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.

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.

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.


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.

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.

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.

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.


Comment
Article Tags:
Article Tags:

Explore