Shreyas Kulkarni
Shreyas' Blog

Shreyas' Blog

User-defined functions in R Explained

User-defined functions in R Explained

Shreyas Kulkarni
·Sep 17, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

let me start with the very basic understanding of functions, functions are basically ready written code which we can call whenever we need it to use. so as usual there are 2 types

  1. Inbuilt functions (R itself have a lot of inbuilt functions)
  2. User-defined function (User writes it for future use)

How R handles functions:

The R Programming language considers these functions as an object and provides them the temporary access of interpreter at the time of execution. Once the function is done with the task provided, the control gets reverted to the interpreter as it was before.

R has a rich library of inbuilt functions which make life easy for an analyst. However, if you find any certain task can be automated using a function, you are free to write a function of your own under the R environment.

Syntax of writing functions in R

function_name - specifies the name of the function. You need to store a function somewhere.

argument_list - specifies the list of argument/s we use under a function.

expressions - specifies the expressions which get executed under a function body to have the required task done.

return -allows you to return a value for the defined function. If we don’t specify return while defining a function, the last line of code inside the function will get executed.

Defining a function to add four numbers

add <- function(a,b,c,d){
  result = a + b + c + d
  print(paste("addition is ", result))
}
  add(1,2,3,4)

#Returns "addition is  10"
  1. here we have defined our function with the name add which consists of 4 arguments.
  2. inside the function body we have created a variable called result which holds the addition of 4 values
  3. in the end we have to print and paste so that we can print out our results
  4. we are giving inputs to the functions and after execution, we will get the results.

Defining function that takes user inputs

This can make your defined function more user-friendly. below function asks the user for input for values for variables the function becomes more generalized a, b, c since the user can provide the value of his interest for a, b, c.

# Function in R with user input

product <- function(a, b, c)
{
  a <- readline("Please enter value for a: ")
  b <- readline("Please enter value for b: ")
  c <- readline("Please enter value for c: ")

  #convert characters into integers
  a <- as.integer(a)
  b <- as.integer(b)
  c <- as.integer(c)

  product = a * b * c

  print(paste("Product of given values is ", product))
}

product(a,b,c)

#Returns:

# product(a,b,c)
#Please enter value for a: 5
#Please enter value for b: 2
#Please enter value for c: 3
#"Product of given values is  30"

So we have seen how to write a function to automate some random tasks, like a calculator can do the same in no time, but to understand a bit about functions we should always start with a familiar field.

hope you are enjoying it, I am planning to write more often because while writing it makes things more clear so let's make something interesting in the next writing till then stay subscribed.

 
Share this