ADVERTISEMENT
ADVERTISEMENT

Home » R Language

Functions in R programming language (with Examples)

R language | functions: In this tutorial, we are going to learn about the functions in R programming language with the declaration, definitions and examples.
Submitted by Bhavya Sri Khandrika, on April 02, 2020

R language functions

As in the other programming languages like C, C++, Java, Python, etc., we find the usage of the Functions in the R language too. But what exactly do these functions mean? How do they help the programmers in the process of developing the best code for a given problem?

Going forward let us define what exactly the word function means?

In general, the functions are nothing but a set of statements that are well organized concurrently to perform a specific task. One must appreciate the beauty of the R language. This is because the R language provides a comfortable surrounding for the users to work on any kind of problem. For this purpose, several functions are inbuilt in the R language. Thus, the programmer will get a chance to expose himself or herself to the wide platform of the R language where one can witness the usage of numerous functions.

Traditionally, the functions that are widely used in the R language are considered to be objects. Having such an assumption it becomes easier for the R interpreter to pass the control in the program. Also, the interpreter enables the users with the option of passing the arguments when the control is transferred to other blocks of code in the entire program. Since the option of passing the arguments makes the programmer very convenient while extending the basic program to a big project.

On the other hand, when the functions are used in the program when that particular statement gets executed then the transfer of control takes place. But eventually, the functions take the responsibility of returning the control to the place from where it is transferred once after the specific action is done. Thus, the function retrievals control to the interpreter once the main task is accomplished. Thereby the final result will be stored with the help of objects.

Syntax of function declaration/definition

A function in the R language is normally defined with the help of a keyword called function.

The general syntax followed for creating a function is as follows:

    function_name <- function(arg_1, arg_2, ...) {
        Function body 
    }

Types of Function Components

When a function is created then the user needs to take a look over the different parameters that eventually give rise to the well-structured function in a program. The following are the prime parameters that need to exemplify while working with the creation of a function in the program concerning any programming language.

They are:

  1. Function Name
  2. Arguments
  3. Function Body
  4. Return Value

a. Function name

It is the actual name given to a function while declaring it. It is mostly stored in the environment, which is available in the R language in the form of an object under the name given to it

b. Arguments

Arguments are defined as placeholders in simple terms. In general, according to the requirements of the programmer, the arguments are passed before the function is invoked. Of course, these arguments are optional too. On the other hand, these arguments can also possess the default values when executed.

c. Function Body

The function body is the phase of a program where there are several statements that are needed to be executed when the function is called.

d. Return Value

The return value is the last stage of any function. In this particular phase, this statement tells about the value that is being returned to the place where the function is called.

In the R language, there are numerous built-in functions. Thus, these can be directly used without defining at the beginning of the program. On the other hand, there is another sub-branch under these functions, i.e., User-defined functions. These types of functions are commonly created, declared and thus finally used according to the requirements of the user.

i) Built-In Functions

Most commonly used functions and of course, the built-in functions in the R language are as follows:

  • seq()
  • mean()
  • max()
  • sum(x)
  • paste()

The above functions are directly called by the programmer in the program.

Here is one of the simplest codes that demonstrates the usage of some of the above-stated functions.

# Create a sequence of numbers from 25 to 30.
print(seq(25,30))

# Find the mean of numbers from 15 to 21.
print(mean(15:21))

# Find the sum of numbers from1 to 10.
print(sum(1:10))

Output

[1] 25 26 27 28 29 30
[1] 18
[1] 55

ii) User Defined Function

Even the R language supports the creation of user-defined functions. The user-defined functions demonstrate the actual need of the programmer. Thus, as per the specific need, the users can go for these user-defined functions. Once the user-defined functions are developed then from the next second they can be used as the built-in functions when the programs are written.

# Create a function to print squares of numbers in sequence.
new.function <- function(c) {
   for(i in 1:c) {
      b <- i^2
      print(b)
   }
}

Calling Function

Below is the code that demonstrates the concept of calling a function in the R language:

# Create a function to print squares of numbers in sequence.
new.function <- function(d) {
   for(i in 1:d) {
      b <- i^2
      print(b)
   }
}

# Call the function new.function supplying 10 as an argument.
new.function(10)

Output

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81
[1] 100

Calling a Function without an Argument

Consider the following code,

# Create a function without an argument.
new.function <- function() {
   for(i in 1:6) {
      print(i^2)
   }
}  

# Call the function without supplying an argument.
new.function()

Output

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

Calling a Function with Argument Values (By Position and By Name)

The arguments that are mentioned during the function call are supplied to the function in the same sequence as that declared at the time when it is called.

# Create a function with arguments.
new.function <- function(a,b,c) {
   result <- a * b + c
   print(result)
}

# Call the function by position of arguments.
new.function(2,1,4)

# Call the function by names of the arguments.
new.function(a = 9, b = 1, c = 3)

Output

[1] 6
[1] 12

Calling a Function with Default Arguments

Also, we have a provision where the user can enter the values of the parameters that are passed during the function definition. Besides that, a function can also be called without passing any arguments in it. Thus, such types of functions fall under the category of calling a function with the default argument.

# Create a function with arguments.
new.function <- function(a = 2, b = 4) {
   result <- a * b
   print(result)
}

# Call the function without giving any argument.
new.function()

# Call the function by giving new values of the argument.
new.function(2,5)

Output

[1] 8
[1] 10

On the other hand, there is another type of evaluation for any function. The arguments that are passed in functions mostly undergo a lazy evaluation. This word may surprise you, people, right? In simple terms, it can be explained as commonly the arguments in the function body get evaluated only in case when they are needed to be. The below code will help you in understanding the concept of lazy evaluation of the function.

# Create a function with arguments.
new.function <- function(a, b) {
   print(a^2)
   print(a)
   print(b)
}

# Evaluate the function without supplying one of the arguments.
new.function(5)

Output

Error(s), warning(s):
Error in print(b) : argument "b" is missing, with no default
Calls: new.function -> print
Execution halted

[1] 25
[1] 5

Thus, the above is the description of the functions and their usage in the R language.

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.