Function In R Programming

In this tutorial, we going to look at functions in r programming also going to learn arguments, this tutorial is to continue of the previous article. Here, you can access the previous article.

What Is Function And Why We Use Function?

functions are blocks of code that execute a basic piece of code without rewriting it. For example, you can always type a + b to sum, or you can easily get it done with the addition function.

Functions are the structures that organize the code. They make the code more readable and facilitate continuous work. They will constantly avoid the hassle of copy and paste because you can use it to call your code blocks.

Function Syntax In R
functionName <- function(arguments)
{
   #Code
}

In this syntax, the name of the function is written first, then the function keyword and arguments are added. Let’s examine a simple function without arguments then we going to create a new function with arguments.

HelloWorld <- function()
{
   print("Hello World!")
}
HelloWorld()

After creating the function, you can use the name and parentheses to call it just like any other function. This function will only print hello world on the screen when it is called, but in the following stages, we will do more specific tasks such as printing the values entered by the user on the screen.

Write <- function(message)
{
   message
}
Write("Hello World!")

The function here works like a print function, the value given to the message variable is printed on the screen. Let’s take a matrix value and print the average of this matrix on the screen.

meanMatrix <- function(matrixValue = matrix())
{
   mean(matrixValue)
}
meanMatrix(matrix(1:6 , ncol = 2 , nrow = 3))

The above function takes the average of the matrix it contains. Now let’s write a function that will perform the desired operation then, we will learn a few practical methods.

operationMatrix <- function(matrixValue = matrix() , operation)
{
   operation(matrixValue)
}
operationMatrix(matrix(1:16 , ncol = 4 , nrow = 4) , sum)
operationMatrix(matrix(1:16 , ncol = 4 , nrow = 4) , mean)

In the example above, we performed two operations on only one function. You can write all the operations you can think of. We will now look at a few ways to make code blocks smoother and easier to read.

Different Styles On Functions

There are a few different styles useful for shortcodes. Your functions don’t always have to have 100’s lines. Let’s look at these styles.

Without Curly Brackets

Example <- function(x) x

For single-line codes like this one, you don’t need to use braces just add your code block next to it (please don’t fit everything in one line)

Curly Brackets (One Line)

Example <- function() {print("Hello World")}

If you are confused, you can still use fancy brackets, and you can write your single line codes in the same way.

Example Functions (Easy And Medium)

CONGRATULATIONS, YOU FINISHED FUNCTIONS IN R PROGRAMMING!

Leave a Reply

Your email address will not be published. Required fields are marked *