## Functions

A function is a block of organized and reusable code that is used to perform a specific task in a program. A function is created by using the function keyword.
The basic syntax of a function is given below:

```FunctionName <- function(arg1, arg2, ...) {
Function Body
}```

Want to get certified in R! Learn R from top R experts and excel in your career with Intellipaat’s R Programming certification!

where,

• FunctionName is the name of a function that is stored as an R object.
• Arguments are used to provide specific inputs to a function while a function is invoked. A function can have zero, single, multiple, or default arguments.
• Function Body contains the block of code that performs the specific task assigned to a function.
• Return Value is the value that a function returns when it succeeds in performing the task. We can also make a function with no return value. So now that we have learned what functions in R programming are, let’s have a quick rundown of all the topics that will be covered in this tutorial

There are two types of functions in R:

• In-built functions
• User-defined functions

We have the perfect professional R Programming Training Course for you!

### In-built Functions

These functions in R programming are provided by R environment for direct execution, to make our work easier.
Some examples for the frequently used in-built functions are as follows:

```#seq() To create a sequence of numbers
print(seq(1,9))```

Output:

` 1 2 3 4 5 6 7 8 9`
```#sum() To find the sum of numbers
print(sum(25,50))```

Output:

`  75`
```#mean() To find the mean of numbers
print(mean(41:68))```

Output:  54.5

```#paste() To combine vectors after converting them to characters
paste(1,"sam",2,"rob",3,"max")```

Output:

` "1 sam 2 rob 3 max"`
`paste(1,"sam",2,"rob",3,"max", sep = ',')      #sep is used to separate the values`

We have the perfect professional R Programming Training in Toronto for you!

Output:

`  "1,sam,2,rob,3,max"`
```paste(1:3, c("sam","rob","max"), sep = '-', collapse = " and ")
#collapse is used to separate the values when a vector is passed```

Output:

`  "1-sam and 2-rob and 3-max"`
```#paste0() Has default sep=“” argument
paste0(1,"sam",2,"rob",3,"max")```

Output:

`  "1sam2rob3max"`
`paste0(1:3, c("sam","rob","max"), collapse = " and ")`

Output:

`  "1sam and 2rob and 3max"`
```head(x,n) #To retrieve the first n rows of a matrix, data frame, or a vector
x = data frame, matrix, or vector
n = number of rows to be retrieved
empid <- c(1:4)
empname <- c("Sam","Rob","Max","John")
empdept <- c("Sales","Marketing","HR","R & D")
emp.data <- data.frame(empid,empname,empdept)

Output:

```           empid     empname       empdept
1           1          Sam            Sales
2           2          Rob        Marketing
3           3          Max             HR```
```tail(x,n) #To retrieve the last n rows of a matrix, data frame, or a vector
x = data frame, matrix, or vector
n = number of rows to be retrieved
empid <- c(1:4)
empname <- c("Sam","Rob","Max","John")
empdept <- c("Sales","Marketing","HR","R & D")
emp.data <- data.frame(empid,empname,empdept)
print(tail(emp.data,2))```

Output:

```         empid      empname        empdept
3          3          Max            HR
4          4          John         R & D```
```min() To return the minimum value from a vector.
x <- c(3,45,6,7,89,9)print(min(x))
#max() To return the maximum value from a vector
x <- c(3,45,6,7,89,9)
print(max(x))```

Output:

``` 3
 89```
```#range() To return the minimum and maximum values from a vector
x <- c(3,45,6,7,89,9)
range(x)
x <- c(3,45,-6,7,89,Inf,9)
range(x)
x <- c(3,45,-6,7,NA,89,Inf,9)
range(x)
x<- c(3,45,-6,7,NA,89,Inf,9)
range(x, na.rm = TRUE)            To remove NA from the result (na.rm) is used```

Output:

```  3 89
  -6 Inf
 NA NA
  -6 Inf```
```#which.min() To return the index of the minimum value from a vector
x <- c(3,45,6,7,89,9)
print(which.min(x))
#which.max() To return the index of the maximum value from a vector
x <- c(3,45,6,7,89,9)
print(which.max(x))```

Output:

``` 1
 5```

Wish to crack Big Data job interviews? Intellipaat’s Top R Programming Interview Questions are meant only for you! ### User-defined Functions

These functions in R programming language are declared, and defined by a user according to the requirements, to perform a specific task.
For example:

```#Create a function to print the sum of squares of numbers in sequence
sum = 0
Function1 <- function(x) {
for(i in 1:x) {
a <- i^2
sum = sum + a
print(sum)
}
}
#Calling a function
Function1(5)```

Output:

``` 1
 5
 14
 30
 55```
```#Function without an Argument
sum = 0
Function1 <- function() {
for(i in 1:5) {
a <- i^2
sum = sum + a
print(sum)
}
}
#Calling a function
Function1()```

Output:

``` 1
 5
 14
 30
 55```
```#Create a function to print the sum of squares of three numbers
Function1 <- function(x,y,z) {
sum = x^2 + y^2 + z^2
print(sum)
}
}
#Calling a function
Function1(2,4,6)                                    #Call by position of arguments
Function1(x=3,y=5,z=7)                       #Call by names of arguments```

Output:

``` 56
 83```
```Function1 <- function(x=4,y=5,z=6) {
sum = x^2 + y^2 + z^2
print(sum)
}
#Calling a function
Function1()                  #Calling a function with default arguments
Function1(6,7,8)             #Passing new values to replace default arguments```

If you have any doubts or queries related to R Programming, get them clarified from the R experts on our R Programming Community!

Output:

``` 77
 149```

In this tutorial we learned what functions in R programming are, the basic syntax of functions in R programming, in-built functions and how to use them to make our work easier, the syntax of a user-defined function, and different types of user-defined functions. In the next session, we are going to learn how to read files in R programming.

For the best of career growth, check out Intellipaat’s R Programming training in Hyderabad and get certified!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7369807,"math_prob":0.9846259,"size":5595,"snap":"2019-51-2020-05","text_gpt3_token_len":1649,"char_repetition_ratio":0.15167233,"word_repetition_ratio":0.19633226,"special_character_ratio":0.32350314,"punctuation_ratio":0.15644655,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9955935,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-24T19:37:55Z\",\"WARC-Record-ID\":\"<urn:uuid:fb3fe58a-491b-4383-8712-9f93eca101a6>\",\"Content-Length\":\"49640\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6feec994-7ee9-40d3-afe8-fd50b6d0bcf3>\",\"WARC-Concurrent-To\":\"<urn:uuid:a156ce10-00e5-4b41-94a1-b29a516289cb>\",\"WARC-IP-Address\":\"104.20.37.203\",\"WARC-Target-URI\":\"https://intellipaat.com/blog/tutorial/r-programming/functions/\",\"WARC-Payload-Digest\":\"sha1:MELJA3SDUAGTUK26PWI6M2LIUOYRKO5G\",\"WARC-Block-Digest\":\"sha1:ES7DEI5545Y2T2E7TJ4D66AJHM4XW2YK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250625097.75_warc_CC-MAIN-20200124191133-20200124220133-00286.warc.gz\"}"}