Functions in R Programming is a block of code or some logic wrapped inside the curly braces { }, which performs a specific operation. We have already seen some methods in this tutorial journey, and you may not notice them.

For instance, print, abs, sqrt, etc., are some of the built-in functions in the R Programming language, and the syntax of it is

Name <- function(arguments) { Local Variable Declaration; Logic; Executable Statement 1; …… Executable Statement n; }

Arguments are:

- Name: It can be any name you wish to give. Avoid using the system reserved keywords.
- Arguments: Every method accepts 0 or more arguments, which completely depends on 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-ins are supported by the Language, or the packages called a Library function. It would be best if you were not worried about the logic inside the Library methods. In our previous articles, We used many library methods such as print(), sqrt(), etc.
- User Defined: Instead of relying only on built-in, this Programming allows us to create our own called user-defined functions. For example, if we want to calculate the Sales profits or any mathematical calculations. Then we can place them in separate methods with a proper name, and later we can call that multiple times.

### Advantages of functions

- 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 methods to each of them.
- We can call the same method multiple times (over and over).
- Code re-usability: Prevent us from writing the same logic multiple times.

## R Functions Declaration

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

add <- function(x, y) { }

### Calling the Function

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

Typical way – In detail

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

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

## Sum and Average of 3 Numbers

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)

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)

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

The below statement asks 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 calculate 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 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))