Functions are useful when you want to perform a certain task multiple number of times. A function accepts input arguments and produces the output by executing valid R commands that are inside the function. In R when you are creating a function the function name and the file in which you are creating the function need not be the same and you can have one or more function definitions in a single R file.
Functions are created in R by using the command
function(). The general structure of the function file is as follows:
Note: In the above syntax f is the function name, this means that you are creating a function with name f which takes certain arguments and executes the following statements.
R provides built-in functions like print(), cat(), etc. but we can also create our own functions. These functions are called user-defined functions.
 "even"  "odd"
Single Input Single Output
Now create a function in R which will take a single input and gives us a single output.
Following is an example to create a function that calculates the area of a circle which takes in the arguments the radius. So, to create a function, name the function as “areaOfCircle” and the arguments that are needed to be passed are the “radius” of the circle.
Multiple Input Multiple Output
Now create a function in R which will take multiple inputs and gives us multiple outputs using a list.
The functions in R takes multiple input objects but returned only one object as output, this is, however, not a limitation because you can create lists of all the outputs which you want to create and once the list is created you can access them into the elements of the list and get the answers which you want.
Let us consider this example to create a function “Rectangle” which takes “length” and “width” of the rectangle and returns area and perimeter of that rectangle. Since R can return only one object. Hence, create one object which is a list that contains “area” and “perimeter” and return the list.
$Area  6 $Perimeter  10
Sometimes creating an R script file, loading it, executing it is a lot of work when you want to just create a very small function. So, what we can do in this kind of situation is an inline function.
To create an inline function you have to use the function command with the argument x and then the expression of the function.
65.33333 15.33333 0
Passing arguments to functions
There are several ways you can pass the arguments to the function:
- Case 1: Generally in R, the arguments are passed to the function in the same order as in the function definition.
- Case 2: If you do not want to follow any order what you can do is you can pass the arguments using the names of the arguments in any order.
- Case 3: If the arguments are not passed the default values are used to execute the function.
Now, let us see the examples for each of these cases in the following R code:
6 32 20
Lazy evaluations of functions in R
In R the functions are executed in a lazy fashion. When we say lazy what it means is if some arguments are missing the function is still executed as long as the execution does not involve those arguments.
In the function “Cylinder” given below. There are defined three-argument “diameter”, “length” and “radius” in the function and the volume calculation does not involve this argument “radius” in this calculation. Now, when you pass this argument “diameter” and “length” even though you are not passing this “radius” the function will still execute because this radius is not used in the calculations inside the function.
Let’s illustrate this in an R code given below:
If you do not pass the argument and then use it in the definition of the function it will throw an error that this “radius” is not passed and it is being used in the function definition.
Error in print(radius) : argument "radius" is missing, with no default
- Poisson Functions in R Programming
- Poisson Functions in R Programming
- Recursive Functions in R Programming
- Conversion Functions in R Programming
- Logarithmic and Power Functions in R Programming
- Performing Logarithmic Computations in R Programming - log(), log10(), log1p(), and log2() Functions
- Exponential Distribution in R Programming - dexp(), pexp(), qexp(), and rexp() Functions
- Gamma Distribution in R Programming - dgamma(), pgamma(), qgamma(), and rgamma() Functions
- Compute Beta Distribution in R Programming - dbeta(), pbeta(), qbeta(), and rbeta() Functions
- Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Get Summary of Results produced by Functions in R Programming - summary() Function
- Inverse functions and composition of functions
- Difference between Functional Programming and Object Oriented Programming
- JS++ | Functions
- ES6 | Functions
- Functions in Python
- Perl | File I/O Functions
- Scala | Functions Call-by-Name
- Perl | List Functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.