## Introduction to Function

Functions are the fundamental building block of any programming language. It helps in modular programming, i.e., we create a block of code(function) and use it whenever required. The creation of function avoids writing the same piece of code again and again. In this topic, we are going to learn about R Program Functions.

A function should be

- written to carry out a specified task.
- may or may not include arguments
- contain a body
- may or may not return one or more values.

### Functions in R

R has many built-in functions which are used for the specific tasks

Here some important and frequently used functions in Data Science

are listed below

#### 1. mean ()

It is used to find the mean for the object.

`Ex: a<-c(0:10, 40)`

xm<-mean(a)

print(xm)

**Output:**

#### 2. sd ()

It returns the standard deviation of an object.

`a<-c(0:10, 40)`

xm<-sd(a)

print(xm)

**Output:**

#### 3. median()

It returns median.

`a<-c(0:10, 40)`

xm<-meadian(a)

print(xm)

**Output:**

#### 4. sum()

It returns sum.

`a<-c(0:10, 40)`

xm<-sum(a)

print(xm)

**Output:**

#### 5. min()

It returns minimum value.

`a<-c(0:10, 40)`

xm<-min(a)

print(xm)

**Output:**

#### 6. max()

It returns maximum value.

`a<-c(0:10, 40)`

xm<-max(a)

print(xm)

**Output:**

#### 7. is.na ()

It returns the empty rows.

The output is either TRUE OR FALSE.

True for empty rows and False for nonempty ones.

- which (is.na ())- It returns the index of the empty rows.
- help () – used to display the documentation of modules, functions, classes, keywords, etc.

There are many other built-in functions that can be used by importing respective libraries.

4.8 (3,335 ratings)

View Course

Apart from these built-in functions, we can create our own functions as per the need.

### Creating Our Own Functions

Here is the format for writing our own function:

`Funtion_name <- function(p)`

{

Body

return ()

}

Here I am explaining each component of this user-defined function.

#### 1. Function_name

We can give any name to our function but we decide the function name based on the

Functionality, i.e., the type of operation it performing.

For example, if we are creating a function to calculate the sum of 2 numbers then

It’s better to give the name “Sum “ to that function.

#### 2. Body of the Function

We write the steps to perform certain operation these steps are termed as the body of the function. The code of the function is closed under curly braces {}.

**For Example**

Suppose we have to calculate the sum of two numbers:

Then the body of the function will be :

`Sum (x,y)`

{

a=x

b=y

c = a+b

return (c)

}

The highlighted lines are termed as the body of the function.

Now, we have come across a few new terms like return () and after the name of the function, we have passed two values x, y these are termed as parameters. I will be explaining these terms in details:

First, Parameters: These are the variables on which we perform the operation defined in the Function.

Second, return (): Inside the function, we have a return () which causes our function to exit and hand back value to its caller.

### Importance to Build the Function

It is very difficult to understand the big chunk of code. It is necessary to devise a new way to break the big monolithic code in smaller readable code, i.e., (Function)

Due to the use of Function, It became a better way to modularize. The function is just another way to group the execution line of codes in one chunk and name it. The name helps us to call it the way you can call me if you know my name.

As we have seen, there are several inbuilt functions in R, which make our

Work easier, we just have to import the libraries and can use the functions

available in these libraries.

### Conclusion – R Program Functions

The primary uses of R are and will always be, statistics, visualization, and machine learning, which requires a lot of calculations and visualizations meaning we will require a lot of functions. Few statistical calculations like mean, median, standard deviation, etc. are required in almost all Data Science projects that’s why we have a lot of inbuilt libraries that consist of many functions that are used frequently. If we need new functionality to be implemented, we can create our own functions.

### Recommended Articles

This is a guide to R Program Functions. Here we discuss some important and frequently used functions in R Program and the format for writing our own function. You may also have a look at the following articles to learn more –