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 functions, 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; }

Function 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 function. Then we can declare those variables inside the function. Remember, these variables are available to this particular function only; we can’t access them outside this function.**Logic:**Any mathematical or any calculations you want to implement.**Executable Statement:**Any print statements to print some data from this particular function.

### R Function Types

There are two types of functions in R Programming language:

**Library Functions:**All the built-in functions supported by the R Language, or the R 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 functions such as print(), sqrt(), etc.**User Defined Functions:**Instead of relying only on built-in functions, R Programming allows us to create our functions 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 function name, and later we can call that function multiple times.

### Advantages of functions in R Programming

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

## R Functions implementation

To implement the functions, we have followed a few rules such as:

### Function Declaration

We can declare the function a following:

add <- function(x, y) { }

### Calling the Function in R Programming

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

**NOTE:** User-defined function name should exactly match with the calling function.

### R Function Definition

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

For example,

# Example For R Functions add.numbers <- function(a, b) { return(a + b) } add.numbers(10, 2)

Typical way – In detail

# Example For R Functions add.numbers <- function(a, b) { Sum <- 0 Sum = a + b return (Sum) } add.numbers(50, 70)

**NOTE:** If you defined the function 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.

# R Functions Example 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)

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

# R FUnctions Example 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)

First, we declared the function by assigning the name sum.numbers. If you forget this function 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** function

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