User Defined Functions


April 13, 2022, Learn eTutorial
1584

Apart from the built-in function, R programming allows its user to create their own functions to meet their programming requirements. The syntax to be followed while creating a user-defined function is the same basic syntax that we discussed under the topic “How to create a function in R?”

The R programming language provides different ways to call a user-created function from anywhere in the program. The different ways to call a function in R are

  • function call without argument in R 
  • function call with arguments in R 
  • function call with default argument

In the next sections, you are going to explore the different ways of handling function calls in the R programming language.

How to call a function in R?

In R programming language a function can be called with, without, or default arguments. Let us go through each case to understand how to use them in practice. These are categorized as user-defined functions.

1. function call without argument in R

A function is created without providing any arguments the execution of the code determines the result.

A function is able to invoke without passing any arguments within the parentheses (). The function() parentheses will be empty.

Syntax


function_name = function(){
     #body_of_function                                                     
}

Let us learn with a few practical examples

The function ls() does not have any arguments inside the parentheses. The execution of ls() provides all the current objects in the R environment. The snippet below shows what it returns after execution of ls()in the current using RStudio.

ls fn snippet

It returns a list of objects created in earlier programs like add, circle, Mul, r, etc. So there is a need for functions without parameters or arguments to perform certain scenarios in a program.


#Create a function website 
website = function(){
  print("Learn eTutorials")
}
#call the function 
website()
website()
website()

Consider another example to display a single statement without any argument in a function.

In this example, the function named website() gets executed 3 times because it is called 3 times. Remember no parameters are passed through the function website(). Let us check the output


[1] "Learn eTutorials"
[1] "Learn eTutorials"
[1] "Learn eTutorials"

Another example of creating a user-defined function to calculate the area of a circle.


#function call without an argument
circle = function(){
  for(r in 1:5){
  area = pi *r*r   #pi is constant for 3.14
  print(area)
  }
}

circle()    #No arguments are passed inside while invoking function circle()

The output generated by the function circle() is shown where the function body gets executed 5 times because a for loop is constructed inside the function to meet the condition that the function should execute 5 times for a range of values 1 to 5. When the variable r=1 it provides 3.141593 when r=2 then provides 12.56637 and so on till r satisfies the condition to calculate the area of the circle.


[1] 3.141593
[1] 12.56637
[1] 28.27433
[1] 50.26548
[1] 78.53982

2. function call with arguments in R

A function has some arguments passed through to invoke the execution of the body of functions to provide the desired output.

In the given example the argument r which is equal to 2 is passed inside the circle() function to find the area of a circle.

The syntax it follows is the basic structure to create a function.

Syntax


Function_name = function(arguments){
     #body_of_function                                                     
}

Consider a simple example with a single argument in a function named pow.


pow = function(a){
  power = a*a
  return(power)
}

pow(4)

The function pow(4) defines codes to finds the power of 4 ie 16.The output generated is


[1] 16

Consider another user-defined function that takes a parameter or argument r to find the area of a circle.


#function call with an argument
circle = function(r){
   {
    area = pi *r*r   #pi is constant for 3.14
    
    cat("The area of the circle when r is ", paste(r)," is”, area)
  }
}

circle(2)    #argument  passed inside while invoking function circle()

The Output is


The area of the circle when r is  2  is 12.56637

3. function call with default argument

Some functions can have default arguments, which are passed directly when a function is created or defined in a program.

The syntax is similar to function with an argument except that values of the argument are provided by default while a function is created.


#function call with default argument
circle = function(r=2){                 
  {
    area = pi *r*r   #pi is constant for 3.14
    cat("The area of the circle when r is ",paste(r)," is",area)
  }
}

circle()   #function call without giving an argument

circle(1)  #function call with argument

Output


The area of the circle when r is  2  is 12.56637
> circle(1)
The area of the circle when r is  1  is 3.141593

What is the chaining function or Nested Functions in R?

The nesting of one function inside another function call is a chaining function in R.It can also refer as nesting function calls. A function with another function within it. The outside function takes the result of the inside function as its arguments. 

Syntax


Function_name = function(function as arguments){
     #body_of_function                                                     
}

Consider an example where we define two functions to convert fahrenheit_to_celsius and celsius_to_kelvin


fahrenheit_to_celsius <- function(temp_F) {
  temp_C <- (temp_F - 32) * 5 / 9
  return(temp_C)
}

# boiling point of water
fahrenheit_to_celsius(212)

The output produced is


[1] 100

Next, let us see Celsius to the kelvin conversion function


celsius_to_kelvin <- function(temp_C) {
  temp_K <- temp_C + 273.15
  return(temp_K)
}

# boiling point of water in Kelvin
celsius_to_kelvin(100)


Output


[1] 373.15

How to convert Fahrenheit to Kelvin?

It converts directly from Fahrenheit to Kelvin inside a function by assigning function names to different variables.


fahrenheit_to_kelvin <- function(temp_F) {
  temp_C <- fahrenheit_to_celsius(temp_F)
  temp_K <- celsius_to_kelvin(temp_C)
  return(temp_K)
}

# boiling point of water in Kelvin
fahrenheit_to_kelvin(212)


The output shows the result of the conversion from Fahrenheit to kelvin.


[1] 373.15

By the use of the nesting function in R the same blocks of codes can be executed in a single line as shown


# boiling point of water in Fahrenheit
celsius_to_kelvin(fahrenheit_to_celsius(212))

The output generated is


[1] 373.15