14 Feb R Functions
In this lesson, we will learn about functions in the R programming language. A function is an organized block of reusable code, which avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a function, set the task in it, and call the function multiple times whenever you need it.
We will learn to create and call a function. With that, arguments, default, and multiple arguments are also covered. The return and recursion concepts are also covered.
Create a Function
To create a Function in the R programming language, the function() keyword is used. Let us see the syntax to create a function:
1 2 3 4 5 |
function_name <- function() { # statement to be executed } |
Above, the function_name is the name of the function. Mention any name for the function.
Now, let us see an example following the above syntax:
1 2 3 4 5 |
demo_function <- function() { # statement to be executed } |
Above, the demo_function is the function name.
Call a Function
To call a function in R, just write the function name followed by parentheses. We will call the above demo_function like this:
1 2 3 |
demo_function() |
Let us see an example to create and call a function in R:
1 2 3 4 5 6 7 8 9 |
# Creating a Function demo_function <- function() { print("My first function...") } # Calling the function demo_function() |
Output
1 2 3 |
[1] "My first function..." |
Function Arguments
Set the arguments in an R function after the name of the function. The arguments are added in parentheses, separated by comma. For example:
1 2 3 |
demo_function <- function(marks) { } |
To call a function with an argument:
1 2 3 |
demo_function(95) |
Let us see a complete example to create and call a function with an argument in the R programming language:
1 2 3 4 5 6 7 8 9 10 |
# Creating a Function demo_function <- function(marks) { print("Displaying the marks...") print(marks) } # Calling the function demo_function(95) |
Output
1 2 3 4 |
[1] "Displaying the marks..." [1] 95 |
Therefore, we have passed the marks with the value 95 while calling above.
Multiple Arguments
Set multiple arguments in an R function after the name of the function. While calling in case of multiple arguments, you can either call by the position of arguments or the name of the arguments.
Call by the position of Arguments
Let us first see an example wherein we will call by the position of arguments:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Creating a Function demo_function <- function(marks1, marks2, marks3) { print("Sum of marks...") res <- marks1 + marks2 + marks3 print(res) } # Calling the function by position of arguments demo_function(95, 99, 85) |
Output
1 2 3 4 |
[1] "Sum of marks..." [1] 279 |
Call by the name of Arguments (change the sequence)
Let us first see an example wherein we will call by name of arguments. We can change the sequence while calling:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Creating a Function demo_function <- function(marks1, marks2, marks3) { print("Sum of marks...") res <- marks1 + marks2 + marks3 print(res) } # Calling the function by name of arguments # We have changed the sequence while calling demo_function(marks1 = 99, marks2 = 85, marks3 = 95) |
Output
1 2 3 4 |
[1] "Sum of marks..." [1] 279 |
Default Arguments
If a function in R is called without supplying any argument, then it considers the default value. Let us see an example to understand Default Arguments in R:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Creating a Function demo_function <- function(name = "Amit") { print("Student name...") print(name) } # Calling the function # Default value is considered since argument isn't supplied demo_function() # New value is assigned demo_function("Virat") # New value is assigned demo_function("John") |
Output
1 2 3 4 5 6 7 8 |
[1] "Student name..." [1] "Amit" [1] "Student name..." [1] "Virat" [1] "Student name..." [1] "John" |
Return
We can easily return values in R just like Python, Java, C++, etc. These languages return using the return keyword. However, to return in an R function, we use the return() function.
Let us see an example to understand return in R:
1 2 3 4 5 6 7 8 9 |
demo_function <- function(i, j) { print("Display the sum") return (i + j) } demo_function(10, 20) demo_function(30, 40) |
Output
1 2 3 4 5 6 |
[1] "Display the sum" [1] 30 [1] "Display the sum" [1] 70 |
Recursion
When a function calls itself in R, it is called Recursion. In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, which makes code efficient and reduces LOC.
The following figure demonstrates how recursion works when we calculate Factorial in R with Recursion:
Recursion Example in R
Let us now see how to find the factorial of a number in R with Recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Creating a Function demo_factorial <- function(n) { if (n >= 1) { return(n*demo_factorial(n-1)) # Recursive Calls } else { return (1) # Factorial 0 is 1 } } # function call demo_factorial(5) |
Output
1 2 3 |
[1] 120 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments