R programming - Functions and their types

R programming - Functions and their types

A program for statistical analysis - Part 5

Hitendra K's photo
Hitendra K
·Jun 9, 2022·

4 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

In the last blog, we looked into looping techniques and decision statements like a switch statement etc. This blog can look into functions, a crucial and most important part of any programming language.

Functions

Functions are a set of codes which are used to perform a specific task a required number of times. The function accepts arguments as input and executes the code specified within the function and produces output.

In R, it is an object so that the interpreter passes control to the function with or without arguments and the necessary actions are performed and, returned the control to the interpreter.

In the R language, A function is defined using the Function keyword. The general syntax for declaring a function is as follows :

syntax

Func -> function( argument 1, argument 2, ......){

Function code

}

Func is the name of the function. The naming protocol is the same as naming a variable, as keywords cannot be used, the name should not start with a number etc.

Basic components in a function:

  1. Function Name - It is the name given to the function. The name is stored as an object on its name in the R environment.
  1. Arguments - An argument is a value which is passed to the function for getting the result for the particular input in that particular function. A function can or cannot contain arguments and both ways are fine for the effectiveness of the function.
  1. Body of the function - The body of the function consists of statements which together constitute a function.
  1. Return Value - It is the last expression in the body of the function to be executed.

Types of functions in R:

There are two types of functions in the R language.

User-defined functions - These are the functions which are created by the user for their needs. These cannot be accessed by others unless the owner of the function shares them.

Built-in functions - These are the functions which are already present in the R language. The functions which are built-in are most widely used and very common, so they are already present in the language.

User-defined function -

As we have seen earlier, A user can declare and define the function by using the function keyword. Below is an example of a basic function.

calling a function with an argument


f1 = function(f){

    result <- f*f

    print(result)

}



f1(3)





//output

9

calling a function without an argument


f1 = function(){

    i=3

    result = i*3

}



f1()



//output

9

calling a function with default argument

The default arguments are values declared within the function. If the function call passes arguments, then the function takes the passed values as arguments or else it takes the default arguments.


f1 = function(a=10, b=20)

{

      result = a*b

      print(result)

}



f1()

f1(20,30)



//output

200

600

calling a function with an argument with respect to position

The arguments can be supplied in the sequence of the variables in the function definition or the passing function can send values with variable names specified to it.


f1 = function( x,y,z){

    result = x+y*z

    print(result)

}



f1(1,2,3)



f1(x=3,y=1,z=2)



//output

9

8

Lazy Evaluation of function

The arguments for the function are evaluated when needed by the functioning body.


f1=function(a,b){

    print(a*a)

    print(a+3)

    print(b)

}



f1(5,3)



//output

25

8

3

Function as argument

Let's see how functions can be passed as an argument.


f1=function(a, f2){

      return(f2(a))

}



fun(b(2:3), min)





//output

2

Recursive Function in R Programming

Recursion is the technique where the function calls itself which eventually forms a loop. Recursion is used because it occupies less memory. The recursive function breaks down the problem and solves it and the results are clubbed together to solve the original problem.


//Factorial using Recursion in R



fact=function(a){

        if(x==0 || x==1)

        {

              return(1)

        }

        else

        {

              return(a*fact(x-1)

        }

}



fact(5)



//output

120

Application and features:

-Recursion makes the code shorter

-Recursion is widely used in dynamic programming and in many other algorithms.

-In dynamic programming, for both top-down as well as bottom-up approaches, recursion is vital for performance.

-In recursion, a huge problem is broken down into smaller problems, and these smaller problems are broken down even further until the problem becomes easier, and then the solution becomes easier.

This is it for today. We will look into more built-in functions in the next blog. Follow up for more such blogs.

 
Share this