R Functions

In this lesson, we will learn about functions in the R programming language. A function is an organized block of reusable code, which avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a function, set the task in it, and call the function multiple times whenever you need it.

We will learn to create and call a function. With that, arguments, default, and multiple arguments are also covered. The return and recursion concepts are also covered.

Create a Function

To create a Function in the R programming language, the function() keyword is used. Let us see the syntax to create a function:

Above, the function_name is the name of the function. Mention any name for the function.

Now, let us see an example following the above syntax:

Above, the demo_function is the function name.

Call a Function

To call a function in R, just write the function name followed by parentheses. We will call the above demo_function like this:

Let us see an example to create and call a function in R:

Output

Function Arguments

Set the arguments in an R function after the name of the function. The arguments are added in parentheses, separated by comma. For example:

To call a function with an argument:

Let us see a complete example to create and call a function with an argument in the R programming language:

Output

Therefore, we have passed the marks with the value 95 while calling above.

Multiple Arguments

Set multiple arguments in an R function after the name of the function. While calling in case of multiple arguments, you can either call by the position of arguments or the name of the arguments.

Call by the position of Arguments

Let us first see an example wherein we will call by the position of arguments:

Output

Call by the name of Arguments (change the sequence)

Let us first see an example wherein we will call by name of arguments. We can change the sequence while calling:

Output

Default Arguments

If a function in R is called without supplying any argument, then it considers the default value. Let us see an example to understand Default Arguments in R:

Output

Return

We can easily return values in R just like Python, Java, C++, etc. These languages return using the return keyword. However, to return in an R function, we use the return() function.

Let us see an example to understand return in R:

Output

Recursion

When a function calls itself in R, it is called Recursion. In another sense, with Recursion, a defined function can call itself. Recursion is a programming approach, which makes code efficient and reduces LOC.

The following figure demonstrates how recursion works when we calculate Factorial in R with Recursion:

Recurion in R programming language

Recursion Example in R

Let us now see how to find the factorial of a number in R with Recursion:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

R Min and Max
R Arrays
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading