Functions in R Programming

Functions in R Programming is a block of code or some logic wrapped inside the curly braces { }, which performs a specific operation. In this R Programming tutorial journey, We have already seen some methods, and you may not notice them. For instance, print, abs, sqrt, etc. are some of the built-in functions in the R Programming language.

R Functions Syntax

The syntax of the Functions in R Programming language is

Function_Name <- function(arguments)  {
  Local Variable Declaration;

  Logic;

  Executable Statement 1;

   ……
  Executable Statement n;
}

Arguments are:

  • Function_Name: It can be any name you wish to give. Avoid using the system reserved keywords.
  • Arguments: Every function accepts 0 or more arguments, and it completely depends upon the user requirements. For example, add(2, 3).
  • Local Variable Declaration: Sometimes, we may need some temporary variable to operate within a particular method. Then we can declare those variables inside it. Remember, these variables are available to this only; we can’t access them outside this method.
  • Logic: Any mathematical or any calculations you want to implement.
  • Executable Statement: Any print statements to print some data from this particular method.

R Function Types

There are two types of functions in R Programming language:

  • All the built-in functions supported by the Language, or the packages called a Library function. It would be best if you are no worried about the logic inside the Library functions. In our previous articles, We used many library methods such as print(), sqrt(), etc.
  • User Defined Functions: Instead of relying only on built-in, R Programming allows us to create our own called as user-defined functions. For example, if we want to calculate the Sales profits or any mathematical calculations. Then we can place them in separate functions with a proper name, and later we can call that multiple times.

Advantages of functions in R Programming

  1. R Functions help us to divide the large programs into small groups. So, we can debug the program quicker and better.
  2. Multiple persons can work on the same program by assigning different methods to each of them.
  3. We can call the same method multiple times (over and over).
  4. Code re-usability: Prevent us from writing the same logic multiple times.

R Functions Declaration

To implement this, we have followed a few rules such as declaration, definition, and calling it. We can declare the function a following:

add <- function(x, y) {
}

Calling the Function in R Programming

It is nothing but calling the original method with a valid number of arguments. For example, add (2, 3)

NOTE: User-defined method name should exactly match with the calling it.

R Function Definition

It is the place where we are going to put all the logic, calculations, etc. We can place this definition either Before the main() or after the main().

add.numbers <- function(a, b)
{
  return(a + b)
}
add.numbers(10, 2)
Functions in R Programming 1

Typical way – In detail

add.numbers <- function(a, b)
{
  Sum <- 0
  Sum = a + b
  return (Sum)
}
add.numbers(50, 70)
Functions in R Programming 2

NOTE: If you defined the func other than void return type, Please don’t forget the return keyword.

Sum and Average of 3 Numbers using R Functions

In this R Program, we calculate the Sum and Average of the three numbers.

sum.numbers <- function(a, b, c)
{
  Sum = a + b + c
  Average = Sum/3
  
  print(paste("Sum of ",a, ",", b, ",", c, "is = ", Sum))
  print(paste("Average of ",a, ",", b, ",", c, "is = ", Average))
}
sum.numbers(20, 10, 70)
Functions in R Programming 3

Or we can allow the user to input the Values for a, b, and c

sum.numbers <- function(a, b, c)
{
  a <- readline(prompt="Enter a Value: ")
  b <- readline(prompt="Enter b Value: ")
  c <- readline(prompt="Enter c Value: ")
  
  # convert character into integer
  a <- as.integer(a)
  b <- as.integer(b)
  c <- as.integer(c)
  
  Sum = a + b + c
  Average = Sum/3
  
  print(paste("Sum of ",a, ",", b, ",", c, "is = ", Sum))
  print(paste("Average of ",a, ",", b, ",", c, "is = ", Average))
}
sum.numbers(a, b, c)
Functions in R Programming 4

First, we declared the method by assigning the name sum.numbers. If you forget this declaration, then it throws an error.

sum.numbers <- function(a, b, c)

The below statement ask the user to enter the values for a, b, c

  a <- readline(prompt="Enter a Value: ")
  b <- readline(prompt="Enter b Value: ")
  c <- readline(prompt="Enter c Value: ")

In the next line, we are converting the user entered values to integers using as.integer

  a <- as.integer(a)
  b <- as.integer(b)
  c <- as.integer(c)

Next, we are calculating the Sum and Average of the user-entered values.

  Sum = a + b + c
  Average = Sum/3

In this example, we assigned the values of a, b, c as 10, 20, 30. It means,

  • Sum = a + b + c => 10 + 20 + 30
  • Sum = 60
  • Average = Sum / 3 => 60 / 3
  • Average = 20

The below R print statements prints the sum and average as the output.

print(paste("Sum of ",a, ",", b, ",", c, "is = ", Sum))
print(paste("Average of ",a, ",", b, ",", c, "is = ", Average))