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 already have seen some functions, and you may not notice them. For instance, print, abs(), sqrt() etc. are some of the built-in functions in R programming language.

## R Functions Syntax

The basic syntax of the Functions in R Programming language is as shown below:

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 perform the operation 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 type of 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 are called as a Library function. You no need to worry 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 own 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 function with a proper function name, and later we can call that function multiple time.

### Advantages of functions in R Programming

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

## R Functions implementation

To implement the function in R program we have followed 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 in C Programming

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

**OUTPUT**

Typical way – In detail

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

**OUTPUT**

**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 program, we will 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)

**OUTPUT**

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)

**OUTPUT**

**ANALYSIS**

First, we declared the function by assigning the name sum.numbers. If you forget this function declaration then the compiler will throw an error.

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

Below statement will 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 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
- Sum = 10 + 20 + 30
- Sum = 60
- Average = Sum / 3
- Average = 60 / 3
- Average = 20

Below print statements are used to print 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))

Thank you for Visiting Our Blog